Skip to content

Commit

Permalink
fix: adapter use resolveCastKey instead of resolveColumnName
Browse files Browse the repository at this point in the history
The return value of insert query returns the value of the primary
key and we need to get cast as key of the model primary key
for consumeAdapterResult
  • Loading branch information
thetutlage committed Jan 31, 2020
1 parent 9e330ff commit ca4b0ff
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
"@adonisjs/core": "2.x.x"
},
"devDependencies": {
"@adonisjs/ace": "^6.8.0",
"@adonisjs/ace": "^6.8.1",
"@adonisjs/application": "^1.3.3",
"@adonisjs/fold": "^6.3.0",
"@adonisjs/fold": "^6.3.1",
"@adonisjs/logger": "^1.1.7",
"@adonisjs/mrm-preset": "^2.2.3",
"@adonisjs/profiler": "^1.2.4",
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/Adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class Adapter implements AdapterContract {
const result = await query.insert(attributes)
if (modelConstructor.increments) {
instance.$consumeAdapterResult({
[modelConstructor.$resolveColumnName(modelConstructor.primaryKey)]: result[0],
[modelConstructor.$resolveCastKey(modelConstructor.primaryKey)]: result[0],
})
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/orm/adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,27 @@ test.group('Adapter', (group) => {
const users = await db.from('users').select('*')
assert.lengthOf(users, 1)
})

test('set primary key value when cast key is different from model key', async (assert) => {
class User extends BaseModel {
public static $table = 'users'

@column({ isPrimary: true, castAs: 'id' })
public userId: number

@column()
public username: string
}

User.boot()

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

assert.exists(user.userId)
assert.deepEqual(user.$attributes, { username: 'virk', userId: user.userId })
assert.isFalse(user.isDirty)
assert.isTrue(user.isPersisted)
})
})

0 comments on commit ca4b0ff

Please sign in to comment.