According to the documentation, calling
save() on a model instance, AdonisJs is smart enough to guess whether it should perform an update or create a new database entry.
const oldUserName = params.id const user = await User.find(oldUserName) console.log(user.toJSON()) // Right record const newUserName = 'begueradj' user.username = newUserName // This should perform an update await user.save()
Actually no update is performed (and no instance is created)
usernameis the primary key here (but I think this should not be an issue)
save()performs the update when the primary key is not involved
- This has other pesky implications such as hooks and setters not taking effect when the custom primary key is concerned
I know how to perform an update using qurey builder syntax, but I am curious to know whether this is a bug ?
I think this is the case (bug) and the issue is in the _update() function:
affected = await query .where(this.constructor.primaryKey, this.primaryKeyValue) .ignoreScopes() .update(this)
The culprit line is this one:
because this function searches for the record to update based on the primary key value whereas the new
primaryKeyValue does not exist yet.