sequelize: Not returning id on create
What you are doing?
Using model.create
to insert an item in the database and using .then(item) to capture the inserted item.
BTW: I am using TypeScript!
db.unity.SiteAlias.create(siteAlias).then((addedAlias: ISiteAlias) => {
console.log(addedAlias);
});
Model
export default function (sequelize: Sequelize, dataTypes: DataTypes) {
const SiteAlias = sequelize.define("SiteAlias", {
id: {
type: dataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: {
type: dataTypes.STRING,
unique: true
},
ignore: {
type: dataTypes.BOOLEAN,
defaultValue: false
},
serviceType: {
type: dataTypes.INTEGER,
allowNull: true,
defaultValue: 1
},
siteId: {
type: dataTypes.INTEGER,
allowNull: true
}
}, {
freezeTableName: true
});
return SiteAlias;
}
What do you expect to happen?
I expected the returned object to contain the id of the inserted item.
What is actually happening?
I get the item returned but with an undefined id value:
Dialect: mssql Database version: 11.0.3000.0 Sequelize version: 3.34.4
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 3
- Comments: 22 (2 by maintainers)
Commits related to this issue
- Merge pull request #3 from DanielGG1024/develop feat: debug model — committed to DanielGG1024/twitter-api-2020 by asterchang96 3 years ago
For me, I needed to have the
autoIncrement: true
property on my model:I had the same issue a couple of days ago, my problem was that model needs to have same properties of the migrations. example: in this case, model also needs to have auto-increment property set to true, not only migration. id: { allowNull: false, primaryKey: true, autoIncrement: true, type: DataTypes.INTEGER(10) },
For anyone who lands here, I had come support via Slack from @janmeier. My problem was that the Id column in the database is capitalised, and my model definition was lowercase. So I added
field: 'Id'
to the id property of my model and it to worked!It seems like this problem occurs also when a model is defined as join table between other two models. @skaterdav85 's answer solved the issue in this case.
Hi, I have the same problem, but in my case the column id of my model is equal to database column, and I still have the same error.
One solution to this problem is to have a custom variable (eg. let id = 0) in your server and increment it every time you sign up a new user.
@felixfbecker - can this be re-opened. Its been 8 months, I am using Sequelize v4.28.6 with tedious v2.1.5 now and I am still not getting the id returned on the model returned from the promise for a create!
I have tried every suggestion and gave you everything I possibly can. Can someone do a remote session with me or something?