Adding a column as a part of a composite primary key for pivot table


#1

I have a belongsToMany relationship between 2 models. On the pivot table, I need a date column which thing seems feasible:

const Model = use('Model')

class User extends Model {
  cars () {
    return this
      .belongsToMany('App/Models/Car')
      .pivotTable('car_user')
      .withTimestamps()
  }
module.exports = User

But I want the date (withTimestamps() will create created_at column in car_user table) .

According to the documentation, car_user table:

  • its own default primary key called id
  • a foreign key car_id
  • a foreign key user_id

But I want to alter my pivot table so that its primary key becomes composite of the previous foreign keys and the created_at column.

Here it says:

When fetching the relationships, Lucid only selects the foreign keys from the pivot table. You can make it select other fields by calling withPivot method.

Question: Given my aim to use a composite primary key, what can I do so that Lucid selects also created_at column, which is, I repeat, a part of the primary key?