[英]Adonis JS Primary key value is missing for user
I'm trying to make a migratiion of users model from adonis to postgres but I keep getting this error Primary key value is missing for user
我正在尝试将用户模型从 adonis 迁移到 postgres,但我不断收到此错误Primary key value is missing for user
My model look like this:我的模型看起来像这样:
class User extends Model {
static boot () {
super.boot()
this.addHook('beforeSave', async (userInstance) => {
if (userInstance.dirty.password) {
userInstance.password = await Hash.make(userInstance.password)
}
})
}
tokens () {
return this.hasMany('App/Models/Token')
}
}
module.exports = User
And the migration I'm trying to run is:我尝试运行的迁移是:
/** @type {import('@adonisjs/lucid/src/Schema')} */
const Schema = use('Schema')
class UserSchema extends Schema {
up () {
this.create('users', (table) => {
table.increments()
table.string('username', 80).notNullable().unique()
table.string('email', 254).notNullable().unique()
table.string('password', 60).notNullable()
table.timestamps()
})
}
down () {
this.drop('users')
}
}
module.exports = UserSchema
I tried adding primary()
to table.increments()
asuming it is generating the auto increments id in postgre database.我尝试将primary()
添加到table.increments()
假设它在 postgre 数据库中生成自动增量 ID。 When I check the database the user is sometimes added, but with id's that goes from 2 to 4 and so on当我检查数据库时,有时会添加用户,但 ID 从 2 到 4 等等
The controller looks like this控制器看起来像这样
const User = use('App/Models/User')
class UserController {
async register({request, auth, response}) {
const username = request.input("username")
const email = request.input("email")
const password = request.input("password")
let user = new User()
user.username = username
user.email = email
user.password = password
user = await user.save()
let accessToken = await auth.generate(user)
return response.json({"user": user, "access_token": accessToken})
}
async login({request, auth, response}) {
const email = request.input("email")
const password = request.input("password");
try {
if (await auth.attempt(email, password)) {
let user = await User.findBy('email', email)
let accessToken = await auth.generate(user)
return response.json({"user":user, "access_token": accessToken})
}
}
catch (e) {
return response.json({message: 'You first need to register!'})
}
}
show ({ auth, params }) {
if (auth.user.id !== Number(params.id)) {
return "You cannot see someone else's profile"
}
return auth.user
}
}
module.exports = UserController
table.increments()
need column name as parameter -> table.increments(name)
table.increments()
需要列名作为参数 -> table.increments(name)
Please read official knex documentation : https://knexjs.org/#Schema-increments请阅读 knex 官方文档: https ://knexjs.org/#Schema-increments
Example :例子 :
table.increments('id').primary()
The issue is with问题是
user = await user.save()
that will return true
when user is saved to db, passing that to auth.generate
is what cause that error.当用户保存到数据库时将返回true
,将其传递给auth.generate
是导致该错误的原因。
Just use something like只需使用类似的东西
let success = await user.save()
and check success value after.并在之后检查成功值。 Might want to return the token and 200 status only if user is saved ( success
is true).可能只在保存用户时才返回令牌和 200 状态( success
为真)。
The problem is that you overwrite user
model instance in user = await user.save()
.问题是您在user = await user.save()
覆盖了user
模型实例。
as @Costin say's simply storing result of await user.save()
in new variable will solve the issue.正如@Costin 所说,只需将await user.save()
结果存储在新变量中即可解决问题。
for example:例如:
...
let saved = await user.save()
let accessToken = await auth.generate(user)
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.