Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class QueryGenerator {
fields.push(this.quoteIdentifier(key));

// SERIALS' can't be NULL in postgresql, use DEFAULT where supported
if (modelAttributeMap && modelAttributeMap[key] && modelAttributeMap[key].autoIncrement === true && !value) {
if (modelAttributeMap && modelAttributeMap[key] && modelAttributeMap[key].autoIncrement === true && value == null) {
Comment thread
sdepold marked this conversation as resolved.
if (!this._dialect.supports.autoIncrement.defaultValue) {
fields.splice(-1, 1);
} else if (this._dialect.supports.DEFAULT) {
Expand Down Expand Up @@ -276,7 +276,8 @@ class QueryGenerator {
this._dialect.supports.bulkDefault
&& serials[key] === true
) {
return fieldValueHash[key] || 'DEFAULT';
// fieldValueHashes[key] ?? 'DEFAULT'
return fieldValueHash[key] != null ? fieldValueHash[key] : 'DEFAULT';
Comment thread
sdepold marked this conversation as resolved.
}

return this.escape(fieldValueHash[key], fieldMappedAttributes[key], { context: 'INSERT' });
Expand Down
39 changes: 39 additions & 0 deletions test/unit/sql/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
});

});

it('allow insert primary key with 0', () => {
const M = Support.sequelize.define('m', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
});

expectsql(sql.insertQuery(M.tableName, { id: 0 }, M.rawAttributes),
{
query: {
mssql: 'SET IDENTITY_INSERT [ms] ON; INSERT INTO [ms] ([id]) VALUES ($1); SET IDENTITY_INSERT [ms] OFF;',
postgres: 'INSERT INTO "ms" ("id") VALUES ($1);',
default: 'INSERT INTO `ms` (`id`) VALUES ($1);'
},
bind: [0]
});
});
});

describe('dates', () => {
Expand Down Expand Up @@ -161,5 +181,24 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
sqlite: 'INSERT INTO `users` (`user_name`,`pass_word`) VALUES (\'testuser\',\'12345\') ON CONFLICT (`user_name`) DO UPDATE SET `user_name`=EXCLUDED.`user_name`,`pass_word`=EXCLUDED.`pass_word`,`updated_at`=EXCLUDED.`updated_at`;'
});
});

it('allow bulk insert primary key with 0', () => {
const M = Support.sequelize.define('m', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
}
});

expectsql(sql.bulkInsertQuery(M.tableName, [{ id: 0 }, { id: null }], {}, M.fieldRawAttributesMap),
{
query: {
mssql: 'SET IDENTITY_INSERT [ms] ON; INSERT INTO [ms] DEFAULT VALUES;INSERT INTO [ms] ([id]) VALUES (0),(NULL);; SET IDENTITY_INSERT [ms] OFF;',
postgres: 'INSERT INTO "ms" ("id") VALUES (0),(DEFAULT);',
default: 'INSERT INTO `ms` (`id`) VALUES (0),(NULL);'
}
});
});
});
});