-
-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need opportunity to disable returning 'id' when calling attach to pivot table #287
Comments
You can create your own pivotModel as defined here. http://adonisjs.com/docs/4.0/relationships#_pivotmodel |
Thanks for answer, @thetutlage! 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
|
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 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: 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/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
The text was updated successfully, but these errors were encountered: