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

attach() with pivotPrimaryKey(false) throws error "Cannot read property '0' of undefined" #482

Closed
yariksav opened this issue Aug 17, 2019 · 2 comments
Assignees
Labels
Type: Question Needs clarification

Comments

@yariksav
Copy link

Based on #287 issue, I think I should be reopened.

For disable returning id in pivot table we made possibility to set pivotPrimaryKey null or false

  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

PS: in commit 0cb13bf#diff-6d18536778f7ef94da4ab8fc5ae2fb76R2641 test only check is sql insert correct, but doesn't check real inserting pivot row

@yariksav yariksav changed the title attach() with pivotPrimaryKey(false) throws error "Cannot read property '0' of undefined" when attach() with pivotPrimaryKey(false) throws error "Cannot read property '0' of undefined" Aug 17, 2019
@fsmiamoto
Copy link

I was having a simular issue when trying to insert related models with a pivot table without a primary key:

{ error: column "id" does not exist
    at Connection.parseE (node_modules\pg\lib\connection.js:604:11)
    at Connection.parseMessage (node_modules\pg\lib\connection.js:401:19)        
    at Socket.<anonymous> (node_modules\pg\lib\connection.js:121:22)
    at Socket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
    ...
}

But adding pivotPrimaryKey to the model seemed to solve the problem with both false and null as parameters:

class Cardapio extends Model {
  pratos () {
    return this.belongsToMany('App/Models/Prato').pivotPrimaryKey(null)
  }
}

Now the insert is successful with using attach:

await cardapio.pratos().attach([hasPrato.id])

@thetutlage thetutlage removed the triage label Nov 5, 2019
@thetutlage
Copy link
Member

I believe @fsmiamoto comment does fix the issue

@thetutlage thetutlage self-assigned this Nov 7, 2019
@thetutlage thetutlage added the Type: Question Needs clarification label Nov 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question Needs clarification
Projects
None yet
Development

No branches or pull requests

3 participants