How to populate a table when a user is created

#1

Hello guys, I need a help. I have a users table and when user is created I need to populate a row into user_config.

But by default these data in user_config, in first time is by default coming from global_config table.

Idk how I do that. Hooks?

I tried to do that in my UserController but dont work.

class UserController {
  async store({ request }) {
    const data = request.only([
      "name",
      "last_name",
      "date_birth",
      "cpf",
      "cep",
      "address",
      "uf",
      "city",
      "cellphone",
      "phone",
      "email",
      "password"
    ]);

  //I got the email to search the user after saved
    const email = request.only("email");
    const user = await User.create(data);
    const userAlreadyRegistred = await User.findByOrFail("email", email);

    const userConfig = await Configuration.create(userAlreadyRegistred.id);

    return user;
  }
0 Likes

#2

Hey

I think you should look at this: https://adonisjs.com/docs/4.0/relationships

To summarize though. In your App/Models/User.js file, add a relationship like so:

configuration()
{
     return this.hasOne(‘App/Models/Configuration’);
}

In your App/Models/Configuration.js file, add another relationship:

user()
{
     return this.belongsTo(‘App/Model/User’);
}

Then, in your controller method, after saving the user, you can do:

async store({ request})
{
     // Create and save the user.

     const userConfig = new Configuration()
     // set parameters

     await user.configuration().save(userConfig);
     return response.created(user);
}

Hope this helps :slight_smile:

0 Likes

#3

Hello man, thank you for answer me!

I had made the relationships between user and user_config but not yet this: await user.configuration().save(userConfig); these line is in userController?

0 Likes

#4

Something like that?

class UserController {
  async store({ request }) {
    //Buscamos os campos do corpo da nossa requisição e os armazenamos em um objeto chamado data;

    const data = request.only([
      "name",
      "last_name",
      "date_birth",
      "cpf",
      "cep",
      "address",
      "uf",
      "city",
      "cellphone",
      "phone",
      "email",
      "password"
    ]);

    //Criamos um novo usuário repassando os parâmetros vindos da requisição e salvamos esse novo usuário em uma variável user;

    const user = await User.create(data);

    const userConfig = new Configuration();
    await user.configuration().save(userConfig);

    //Retornamos o novo usuário como resultado da requisição, como selecionamos, no nosso caso o retorno será um JSON.
    return response.created(user);
  }
0 Likes

#5

Yep. That looks about right. Let me know if you are still having problems.

Don’t forget to populate your created Configuration model with the proper values if you haven’t done so.

const userConfig = new Configuration()

only creates an empty model with default values (if specified in migrations).

Be sure to also check out the doc I linked in my original response :slight_smile:

0 Likes

#6

after test in insomnia occurr error:

“message”: “insert into “configurations” (“created_at”, “updated_at”, “user_id”) values ($1, $2, $3) returning “id” - null value in column “warning_expiration” violates not-null constraint”,

My config controller:


async store({ request, response }) {
    const data = request.input("user_id");

    const configuration = await Configuration.create(data);

    return configuration;
  }

Maybe I’m doing something wrong in relationship…?

0 Likes

#7

This was the solution I found:

class UserController {
  async store({ request, response }) {

    const data = request.only([
      "name",
      "last_name",
      "date_birth",
      "cpf",
      "cep",
      "address",
      "uf",
      "city",
      "cellphone",
      "phone",
      "email",
      "password"
    ]);

    const user = await User.create(data);

    await user.configuration().create({ user_id: user.id });

    return response.created(user);
  }
1 Like