You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Exclude id attribute in model if using PostgreSQL? #9
Manually removing the id attribute from the model generated by sequelize-automate appears to get everything working as expected, with sequelize/PostgreSQL automatically populating the id column with an auto-incrementing integer.
Question: If my understanding is correct, sequelize-automate should exclude the id attribute in the models that it generates if using PostgreSQL. Is there an option to do this? Or should sequelize-automate do this automatically?
Hi @athenawisdoms , thanks for your feedback. I have fixed this issue, the new version is sequelize-automate@1.2.0.
PostgreSQL use serial to create auto-increment column: set default value to nextval(${table}_${field_seq::regclass) for auto-increment column. So the sequelize-automate@1.1.1 (old version) just set defaultValue: "nextval(my_data_id_seq::regclass)", and it's incorrect in sequelize.
There are two solutions to solve it:
Solution A: just remove id as you said.
Solution B: set autoIncrement to true and remove defaultValue: "nextval(my_data_id_seq::regclass)",.
I think Solution B is better, it's more semantics and clear.
For example, the postgresql table is
CREATETABLE "user"(
id SERIALPRIMARY KEYNOT NULL,
name TEXTNOT NULL,
age INT
);
and the model is
const{
DataTypes
}=require('sequelize');module.exports=sequelize=>{constattributes={id: {type: DataTypes.INTEGER,allowNull: false,defaultValue: null,comment: null,primaryKey: true,field: "id",// set autoIncrement to true, because the `id` is an auto-increment columnautoIncrement: true},name: {type: DataTypes.TEXT,allowNull: false,defaultValue: null,comment: null,primaryKey: false,field: "name",autoIncrement: false},age: {type: DataTypes.INTEGER,allowNull: true,defaultValue: null,comment: null,primaryKey: false,field: "age",autoIncrement: false}};// ...
Activity
nodejh commentedon Dec 30, 2019
@athenawisdoms I agree with you and I'll fix it.
nodejh commentedon Jan 7, 2020
Hi @athenawisdoms , thanks for your feedback. I have fixed this issue, the new version is
sequelize-automate@1.2.0.PostgreSQL use serial to create auto-increment column: set default value to
nextval(${table}_${field_seq::regclass)for auto-increment column. So thesequelize-automate@1.1.1(old version) just set defaultValue: "nextval(my_data_id_seq::regclass)", and it's incorrect in sequelize.There are two solutions to solve it:
Solution A: just remove id as you said.
Solution B: set
autoIncrementtotrueand removedefaultValue: "nextval(my_data_id_seq::regclass)",.I think Solution B is better, it's more semantics and clear.
For example, the postgresql table is
and the model is
athenawisdoms commentedon Jan 8, 2020
Your solution B is indeed better, and the updated
sequelize-automate@1.2.0fixed this problem.Thank you for the explanation as well. Truly appreciate your excellent work here.