Lucid: Need opportunity to disable returning 'id' when calling attach to pivot table

Created on 24 Jan 2018  路  7Comments  路  Source: adonisjs/lucid

https://github.com/adonisjs/adonis-lucid/blob/22adf6950a747e4b69e0e854c1c866877140731b/src/Lucid/Model/PivotModel.js#L165-L167

Can you make some flag to disable returning id if pivot table don't have it
And i suppose will need opportunity to change name of this column, if some linked table will have for example 'linked_id' name of primary column

Now when I call User.objects().attach(1)- I have error
{ error: column "id" does not exist
at Connection.parseE (/Users/yarik/Projects/processing/backend/node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (/Users/yarik/Projects/processing/backend/node_modules/pg/lib/connection.js:371:19)
at Socket. (/Users/yarik/Projects/processing/backend/node_modules/pg/lib/connection.js:113:22)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)

Because table user_object has only 2 column: user_id and object_id
My relation

  objects () {
    return this
      .belongsToMany('App/Models/Objects', 'user_id', 'object_id')
      .pivotTable('user_object')
  }

Most helpful comment

This probably needs to be reopened, I don't think it's resolved.

It appears that belongsToMany relationships still assume that there will be a primarykey/id field in the pivot table, but a pivot table doesn't need a primary key, it only requires the two foreign keys.

As such, the attach and sync methods generate queries that include a returning id clause, but if an id field is not present on the pivot table, postgres fails. I know for certain this doesn't affect sqlite, nor I think mysql.

While I understand you _can_ use a pivotModel that defines a null primaryKey, it really doesn't seem as though you should _have_ to do so.

In fact, the logic strikes me as reversed.. if all you can about is a simple pivot, all you need are foreign keys, and the assumption should be that the table consists solely of th two foreign keys, whereas if you need something more complex on your pivot, then the user should opt for a pivotModel.

All 7 comments

You can create your own pivotModel as defined here. http://adonisjs.com/docs/4.0/relationships#_pivotmodel

Thanks for answer, @thetutlage!
Creating for all link-tables separate model class is not best idea, beside I'm not sure is model works good with tables without primarykey. (?) And if I'll create PivotModel, will method attach() work?

As for me - using only 'id' column without possibility to change the column name or disable it - not universal logic

PS. I'm not sure is necessary to put primary keys for link-tables, I never use this in my databases. Linked tables have only linked foreign keys.

It would be great to have a function like withPrimaryKey(key), 'id' - will be default, and if send withPrimaryKey(false) - query will run without returning

  objects () {
    return this
      .belongsToMany('App/Models/Objects', 'user_id', 'object_id')
      .pivotTable('user_object').withPrimaryKey(false)
  }

I have the same problem, and I consider that the primary key in a pivot table should be optional

This probably needs to be reopened, I don't think it's resolved.

It appears that belongsToMany relationships still assume that there will be a primarykey/id field in the pivot table, but a pivot table doesn't need a primary key, it only requires the two foreign keys.

As such, the attach and sync methods generate queries that include a returning id clause, but if an id field is not present on the pivot table, postgres fails. I know for certain this doesn't affect sqlite, nor I think mysql.

While I understand you _can_ use a pivotModel that defines a null primaryKey, it really doesn't seem as though you should _have_ to do so.

In fact, the logic strikes me as reversed.. if all you can about is a simple pivot, all you need are foreign keys, and the assumption should be that the table consists solely of th two foreign keys, whereas if you need something more complex on your pivot, then the user should opt for a pivotModel.

+1

  objects () {
    return this
      .belongsToMany('App/Models/TradeObject', 'userinfo_id', 'tradeobject_id')
      .pivotTable('userlink')
      .pivotPrimaryKey(null)
  }
await this.model.objects().attach([this.params.objectId])

provided correct sql insert:

insert into [userlink] ([tradeobject_id], [userinfo_id]) values (?, ?) 

but it throws error:
Cannot read property '0' of undefined

After investigation - model.js always try to get [0] index of result when incrementing=true, but now it's undefined
https://github.com/adonisjs/adonis-lucid/blob/6cd666df9534a981f4bd99ac0812a44d1685728a/src/Lucid/Model/index.js#L620-L622 How to disable incrementing in pivot i don't know

Was this page helpful?
0 / 5 - 0 ratings