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.
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')
}
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
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 idclause, 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.