I have a js object with explicit id
property. Like this:
const data = {
user_id: null,
user_email: faker.internet.email()
};
The value of user_id
is null and there is a users
table using user_id
as its primary key.
I want to insert this data correctly and I hope knex
can obey the primary key increment rule.
Here is my code:
async function insert(user: any) {
return await knex('users')
.insert(user)
.returning('*');
}
When I try to insert this data, got an error:
error: null value in column "user_id" violates not-null constraint
How can I solve this?
You can set the value of user_id
to undefined
. Then it will insert the data and
obey the primary key increment rule.
const data = {
user_id: undefined,
user_email: faker.internet.email()
};
Check the inserted row in users
table. The value of user_id
is 1