Skip to content
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

Closed
yariksav opened this issue Jan 24, 2018 · 7 comments
Closed
Assignees

Comments

@yariksav
Copy link

yariksav commented Jan 24, 2018

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')
  }
@thetutlage
Copy link
Member

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

@yariksav
Copy link
Author

yariksav commented Jan 24, 2018

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.

@yariksav
Copy link
Author

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)
  }

@eBaeza
Copy link

eBaeza commented Apr 20, 2018

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

@willvincent
Copy link

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.

@vjnrv
Copy link

vjnrv commented Aug 14, 2019

+1

@yariksav
Copy link
Author

yariksav commented Aug 17, 2019

  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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants