Skip to content

Commit

Permalink
fix(belongsToMany): pick value of define key over primaryKeyValue
Browse files Browse the repository at this point in the history
fix #246
  • Loading branch information
thetutlage committed Dec 14, 2017
1 parent bfe5de4 commit 7116c2b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Lucid/Relations/BelongsToMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ class BelongsToMany extends BaseRelation {
/**
* Attach the pivot rows
*/
const pivotRows = await this.attach(relatedInstance.primaryKeyValue, pivotCallback)
const pivotRows = await this.attach(relatedInstance[this.relatedPrimaryKey], pivotCallback)

/**
* Set saved pivot row as a relationship
Expand Down
23 changes: 22 additions & 1 deletion test/unit/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ module.exports = {
table.boolean('has_blocked')
table.timestamps()
table.timestamp('deleted_at').nullable()
}),
db.schema.createTable('party_users', function (table) {
table.increments('id')
table.integer('party_id')
table.string('username')
table.timestamps()
}),
db.schema.createTable('teams', function (table) {
table.increments('id')
table.integer('party_id')
table.string('name')
table.timestamps()
}),
db.schema.createTable('team_user', function (table) {
table.increments('id')
table.integer('team_party_id')
table.integer('user_party_id')
table.timestamps()
})
])
},
Expand All @@ -189,7 +207,10 @@ module.exports = {
db.schema.dropTable('categories'),
db.schema.dropTable('sections'),
db.schema.dropTable('post_section'),
db.schema.dropTable('followers')
db.schema.dropTable('followers'),
db.schema.dropTable('party_users'),
db.schema.dropTable('teams'),
db.schema.dropTable('team_user')
])
},

Expand Down
61 changes: 61 additions & 0 deletions test/unit/lucid-belongs-to-many.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ test.group('Relations | Belongs To Many', (group) => {
await ioc.use('Adonis/Src/Database').table('posts').truncate()
await ioc.use('Adonis/Src/Database').table('post_user').truncate()
await ioc.use('Adonis/Src/Database').table('followers').truncate()
await ioc.use('Adonis/Src/Database').table('team_user').truncate()
await ioc.use('Adonis/Src/Database').table('party_users').truncate()
await ioc.use('Adonis/Src/Database').table('teams').truncate()
})

group.after(async () => {
Expand Down Expand Up @@ -2028,4 +2031,62 @@ test.group('Relations | Belongs To Many', (group) => {
const userPosts = user.posts()
assert.equal(userPosts.$pivotTable, 'post_users')
})

test('attach model via different primary key', async (assert) => {
class Team extends Model {
}

class User extends Model {
static get table () {
return 'party_users'
}

teams () {
return this.belongsToMany(Team, 'user_party_id', 'team_party_id', 'party_id', 'party_id')
}
}

User._bootIfNotBooted()
Team._bootIfNotBooted()

const user = new User()
user.username = 'virk'
user.party_id = 20
await user.save()

await user.teams().attach(10)
const pivotValues = await ioc.use('Database').table('team_user')
assert.lengthOf(pivotValues, 1)
assert.equal(pivotValues[0].user_party_id, 20)
assert.equal(pivotValues[0].team_party_id, 10)
})

test('create and attach model via different primary key', async (assert) => {
class Team extends Model {
}

class User extends Model {
static get table () {
return 'party_users'
}

teams () {
return this.belongsToMany(Team, 'user_party_id', 'team_party_id', 'party_id', 'party_id')
}
}

User._bootIfNotBooted()
Team._bootIfNotBooted()

const user = new User()
user.username = 'virk'
user.party_id = 20
await user.save()

await user.teams().create({ name: 'draculas', party_id: 10 })
const pivotValues = await ioc.use('Database').table('team_user')
assert.lengthOf(pivotValues, 1)
assert.equal(pivotValues[0].user_party_id, 20)
assert.equal(pivotValues[0].team_party_id, 10)
})
})

0 comments on commit 7116c2b

Please sign in to comment.