Reputation: 80
I'm setting up a database table using Sequelize-Typescript recently and using this ORM to assist me with database operations.
So I was created a table called uma_tbl_users
with the snippet codes below:
interface UserInterface {
id: number;
... Other attributes...
}
@Table(
{
tableName : 'uma_tbl_users',
timestamps : false,
underscored : true,
schema : 'po_uma'
}
)
class User extends Model implements UserInterface {
@Column({
autoIncrement: true,
primaryKey: true,
field: "user_id",
type: DataTypes.BIGINT
})
id!: number;
... Other Implemented Attributes from the interface
}
After creating this table, I do the same things with other tables and run Sequelize Database Syncer in force mode.
When I run the code to run the CREATE data statement for this table, I'm always receiving this error below:
SequelizeDatabaseError: null value in column \"user_id\" violates not-null constraint
But what went weird is, this issue only occurs for this table. Every other table works just fine. And I've already made sure there is no validation interfering with the create statement.
Here's my create statement:
const execution: User = await User.create({
role_id: request.role,
email: request.email,
phone: request.phone,
password: request.password
});
return execution;
@AutoIncrement
and @PrimaryKey
annotation instead of load all options inside @Column
, but the problem still persists.sync({ force: true })
but the problem still persists.uma_tbl_users
, the ID will return 6 instead of what I've to insert)Upvotes: 2
Views: 2071
Reputation: 184
You are probably supplying the primary keys yourself while seeding this particular table.
Tables with custom PKs and AutoIncrement set to true should not be seeded with explicitly set primary keys.
Just seed the other columns. Sequelize-Typescript will enter the primary keys on its own.
Upvotes: 1
Reputation: 80
I don't know if it's a bug from Sequelize-Typescript or what, but when I change the "user_id"
into "id"
by removing field
parameter, it works.
Final @Column
setting
@Column({
autoIncrement: true,
primaryKey: true,
type: DataTypes.BIGINT
})
id!: number;
Feel free to post your answer/opinion.
Upvotes: 1