Doubt about relationships and field names

Hi! First of all sorry for my bad english.

Well I’m working trying to implement the solution that I receive in this post How can I create an update_by field on a model

But I found a new problem, i see that my field names are not descriptive to my needs, in my projects i have a field updated_by, related to the user that update the register, but the field name of the creator of that register is “user_id”, i try to change it to “created_by” but when i make that i get an error, because Adonis is looking for the field user_id to put the integer (id).

This is my projects schema

up () {
    this.create('projects', (table) => {
      table.increments()
      table.integer('created_by').unsigned().references('id').inTable('users')
      table.integer('updated_by').unsigned().references('id').inTable('users').nullable()
      table.string('title', 80).notNullable().unique().index()
      table.string('description', 254).nullable()
      table.timestamps()
    })
  }

this are my methods in the models users and projects

// PROJECTS
class Manufacturer extends Model {
  
  created_by() {
    return this.belongsTo('App/Models/User')
  }
  
  updated_by() {
    return this.belongsTo('App/Models/User')
  }
}


// USERS
  projects() {
    return this.hasMany('App/Models/Project')
  }
  projects_updated() {
    return this.hasMany('App/Models/Project')
  }

And this is my controller for the store method

async store ({ request, response, auth }) {
    const user = await auth.getUser();
    const { name, description } = request.all();
    const project = new Project();
    project.fill({
      title,
      description
    });
    await user.projects().save(project);
    return response.json(project);
  }

Well when i try to create the register i get this error

insert into manufacturers (created_at, name, phone, updated_at, user_id) … SQLITE_ERROR: table manufacturers has no column named user_id

Then obviously Adonis is looking for the field user_id that I think is a convention to use user_id when a field belongsTo a user but in my case is more descriptive created_by, is any way to make this work changing the field name? And I think i’m going to need this to when I try to update a register and save the user_id in the updated_by field.

And the last thing! I see that this line

projects_updated() {
    return this.hasMany('App/Models/Project')
  }

Is the same of the method projects(), then how can i make that this method only points to the updated_by column and not to the user_id column when i try to fetch the data of the projects updated by X user?

Hope you can understand me, Thanks for your Help!

1 Like

You can specify a custom column name when you define the belongsTo relationship.

Here’s the documentation on how to do this: https://adonisjs.com/docs/4.1/relationships#_belongs_to.

Essentially, what you want to do is add the column name as second argument to let Lucid know that you’re using custom column names:

created_by() {
  return this.belongsTo('App/Models/User', 'created_by')
}

updated_by() {
  return this.belongsTo('App/Models/User', 'updated_by')
}
1 Like

Forgot to answer your last question.

Similar to belongsTo, hasMany also has similar method signatures for specifying custom column names. You can change it to:

projects_updated() {
  return this.hasMany('App/Models/Project', 'id', 'updated_by')
}

For information is in the documentation: https://adonisjs.com/docs/4.1/relationships#_has_many

Hope this helps!

2 Likes