id in dataValues is null after create model (mssql) but does exist elsewhere
See original GitHub issueIssue Description
I use Model.create() to build and save a model instance. Why is report.dataValues.id = null, when the report.id value is populated?
I read multiple other issue spanning from 2014-2019. In those cases they mention:
- make sure autoIncrement is set and spelled correctly. - confirmed it is correct.
- make sure the field name is correctly the same as in the db. - confirmed it is correctly named ‘id’. With and without the field set. bonus: the id field is selectable when I query it and gives the correct record. My Issue seems to be isolated to the create call.
- omit defining id because sequelize does it for you by default. -tried that with no success.
What are you doing?
//model definition below in additions
const report = await Report.create({name: 'test'});
// will yield
report.dataValues.id === null;
report.id: === 43;
//bonus:
it also yields:
report.dataValues.name === 'test';
report.name === undefined;
//for all fields defined on my model.
What do you expect to happen?
I expect report.dataValues.id to have the same value as report.id so that I can
- easily return the created object including the output.id field
- do other stuff with the model.
What is actually happening?
console.log shown below. The dataValues.id is causing other usage of the model methods to break. for instance Model.reload will output the query with the WHERE claus IS NULL:
SELECT [id], [name], [createdAt], [updatedAt]
FROM [Reports] AS [ReportModel]
WHERE [ReportModel].[id] IS NULL
ORDER BY [ReportModel].[id]
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY;
here’s the console.log of the model.create() output:
report ReportModel {
dataValues: {
id: null,
name: 'test',
updatedAt: 2020-01-15T02:47:53.705Z,
createdAt: 2020-01-15T02:47:53.705Z
},
_previousDataValues: {
name: 'test',
id: null,
shortDescription: undefined,
description: undefined,
externalLink: undefined,
createdAt: 2020-01-15T02:47:53.705Z,
updatedAt: 2020-01-15T02:47:53.705Z
},
_changed: {
name: false,
id: false,
createdAt: false,
updatedAt: false
},
_modelOptions: {
timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { id: null },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [],
name: { plural: 'ReportModels', singular: 'ReportModel' },
omitNull: false,
sequelize: Sequelize {
options: [Object],
config: [Object],
dialect: [MssqlDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: {}
},
tableName: 'Reports',
hooks: {}
},
_options: {
isNewRecord: true,
_schema: null,
_schemaDelimiter: '',
attributes: undefined,
include: undefined,
raw: undefined,
silent: undefined
},
isNewRecord: false,
id: 43,
name: undefined,
createdAt: undefined,
updatedAt: undefined
}
Additional context
Model defintion:
//typescript stuff from the docs:
export default class ReportModel extends Model {
public id!: number;
public name!: string;
// timestamps generated by sequelize
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
//model association declarations
}
ReportModel.init(
{
id: {
field: 'id',
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
allowNull: false,
},
name: {
type: new DataTypes.STRING(64),
allowNull: false,
},
},
{
sequelize,
tableName: 'Reports',
},
);
CREATE TABLE statement:
CREATE TABLE Reports (
id int IDENTITY(1,1) PRIMARY KEY,
name varchar(64) not null,
createdAt DATETIME2(7) not null,
updatedAt DATETIME2(7) not null
);
Environment
- Sequelize version: 5.21.2
- Node.js version: 12.13
- Operating System: osx 10.15
- If TypeScript related: TypeScript version: 3.6
Issue Template Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
- I think this problem happens only for the following dialect(s):
- I don’t know, I was using MSSQL, with TEDIUS version 6.6 and database version MSSQL 2014 12.0
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
SQL ISNULL function
This article explores the SQL ISNULL function to replace NULL values in expressions or table records with examples.
Read more >Why NULL Values Should Not Be Used in a Database Unless ...
When you set up a database (at least in MS SQL Server) you can flag a field as allowing NULL values and which...
Read more >Transaction locking and row versioning guide - SQL Server
This guide describes the locking and row versioning mechanisms the SQL Server Database Engine uses to ensure the physical integrity of each ...
Read more >Null (SQL) - Wikipedia
In SQL, null or NULL is a special marker used to indicate that a data value does not exist in the database. Introduced...
Read more >SQL Query with dynamic number of columns - Ask TOM
I think, I have a way to do this. Creating a View Dynamically using Execute Immediate and getting the values from that view....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We had the same issue using dialect mysql.
In the end we solved it by removing
"@babel/plugin-proposal-class-properties"
and addingpublic id!: number;
back to our Model, which seems to have been causing some sort of conflict. We got the solution from this comment: https://github.com/sequelize/sequelize/issues/11675#issuecomment-553783795This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the “stale” label. 🙂