sequelize: using uuid as primary key with auto-generated default value causes not-null constraint violation error
using master branch with postgres:
var User = sequelize.define('User', {
'id': {
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
},
// etc
this does not work unless i remove primaryKey: true
. it causes error: null value in column "id" violates not-null constraint
. it seems like the default value is not being generated when the column/property is a foreign key.
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 1
- Comments: 25 (12 by maintainers)
Sequelize.UUIDV4
is an alias ofDataTypes.UUIDV4
, but yeah stick to DataTypesHowever,
DataTypes.UUIDV4
cannot be used in migrations, it can only be used in Model.init or sequelize.define. This is becauseDataTypes.UUIDV4
is handled in JavaScript, not SQL.Related to https://github.com/sequelize/sequelize/issues/13224
I seem to be having this exact same problem. Relevant snippet of my migration file:
The error I get when trying to insert a new user is this:
Unsure if I have to do something else, I simply followed the documentation. I’m using the latest
sequelize
andpg
versions.@cjroth found updated docs on this.
In your model, use
The older docs say do
defaultValue: Sequelize.UUIDV4
but that’s incorrect. UsedefaultValue: DataTypes.UUIDv4
instead.