sequelize / sequelize/sequelize
MSSQL: no inserted ID returned on raw insert query
Open
Nobody has claimed this yet.
dialect: mssql
status: awaiting response
type: bug
- Dominant language
- TypeScript
- Stars
- 30.4k
- Forks
- 4.3k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 68
Description
import Sequelize = require('sequelize');
import { eachSeries } from 'async';
export function up(
qi: Sequelize.QueryInterface, _: void,
done: ErrorCallback<Error>
) {
return qi.createTable('dangerousSubstances', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
articleId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'articles',
key: 'id',
},
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn('GETDATE'),
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.fn('GETDATE'),
},
deletedAt: {
type: Sequelize.DATE,
allowNull: true,
},
}, {
logging: console.log,
}).then(() => {
var data = [
{
name: 'SHELL RIMULA R5 M10W/40',
},
{
name: 'SHELL RIMULA R5 E10W/40',
},
];
eachSeries(data, (ds, insertDone) => {
qi.sequelize.query(
"INSERT INTO articles (name) VALUES (?);",
{
type: Sequelize.QueryTypes.INSERT,
replacements: [ ds.name ],
logging: console.log,
}
).then(a => {
console.log(a);
// Executing (default): INSERT INTO articles (name) VALUES (N'SHELL RIMULA R5 M10W/40');
// [ [], 1 ] <----- HERE, no INSERTED ID info at all
// Executing (default): INSERT INTO articles (name) VALUES (N'SHELL RIMULA R5 E10W/40');
// [ [], 1 ]
insertDone();
return null;
});
}, done);
});
}
export function down(qi: Sequelize.QueryInterface)
{
return qi.dropTable('dangerousSubstances', {
logging: console.log,
});
}
See comments under console.log... This is just an example and data probably should not be inserted in migration. But I do not know better option, seeds are for test purpose only and I am migrating also existing data to new db...
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the qi.sequelize.query call using QueryTypes.INSERT and compare the MSSQL raw-insert result with the expected inserted ID behavior. Check the existing dialect handling and related query tests, if available; done means a raw MSSQL insert exposes the generated ID without regressing other dialects.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sql, typescript
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100