sequelize / sequelize/sequelize

MSSQL: no inserted ID returned on raw insert query

Open
#7,969 5 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.