Repository navigation
Description
Hi! This library looks like really cool - this is a much better way to declare model-classes for sequelize! Unfortunately i ran into a problem when trying it out:
Versions
-
@babel/[email protected]
-
@babel/[email protected]
-
@babel/[email protected]
-
@babel/[email protected]
I'm submitting a ...
[x] bug report
[ ] feature request
Actual behavior:
I tried converting my existing sequelize code to sequelize-typescript. It worked for the most part but i encountered a difference in behavior.
(Not 100% how to debug this, so i am reporting it as a bug here - could of course be a configuration issue or similar on my end)
The non-static update-function on a Model, normally returns a promise containing a model-object with the updated property values. After changing from plain sequelize to sequelize-typescript, it no longer does this - instead the promise now contains a model-object with the old values from before the update.
(The database is updated with the new values, the problem is just that the return value of the update-method contains different values than it normally does)
When debugging, i can see that the dataValues-object of the model-object are updated with the new values, but the properties of the model-object still contain the old values. (They are not updated)
The same problem happens for the with set + save combo.
Expected behavior:
The non-static update method should return a model-object with updated values, like in plain sequelize. (and same for set+save)
Steps to reproduce:
See code below
Related code:
The model:
import {Table, Column, Model, HasMany, DataType, BelongsTo, BeforeDestroy, DeletedAt, UpdatedAt, CreatedAt} from 'sequelize-typescript';
@Table({
tableName: 'users',
timestamps: true,
underscored: true,
paranoid: true
})
export default class User extends Model<User> {
@Column({type: DataType.INTEGER, field: "id", primaryKey: true, autoIncrement:true})
id: number = this.id; // <- see: https://github.com/sequelize/sequelize/issues/10579 https://github.com/babel/babel/issues/9105
@Column({type: DataType.STRING(100), field: "first_name", allowNull: false})
firstName: string = this.firstName;
}The code to reproduce the problem:
...
const user = await User.findById(userId, { transaction: t });
console.log("Before update: ", user.firstName); // the old value
const updatedUser = await user.update({ firstName: "Something" }, { transaction: t });
console.log("After update: ", updatedUser.firstName); // still the old value
...Same problem happens with:
...
const user = await User.findById(userId, { transaction: t });
console.log("Before update: ", user.firstName); // the old value
user.set({ firstName: "Something" });
console.log("After set: ", user.firstName); // still the old value
const updatedUser = await user.save({ transaction: t });
console.log("After save: ", user.firstName); // still the old value
console.log("After save: ", updatedUser.firstName); // still the old value
...Without sequelize-typescript (i.e. with just plain sequelize without typescript), it works.
It is of course not impossible that it is a babel vs typescript issue.
Could it, for example, be because i'm assigning each property to itself in the model? (for the reason for doing this, see: sequelize/sequelize#10579 and babel/babel#9105 ).
Library configurations:
.babelrc:
{
"presets": [
["@babel/env", {"targets": {"node": "8.0"}}],
"@babel/react",
["@babel/typescript", {"isTSX": true, "allExtensions": true}]
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"moduleResolution": "node",
"allowJs": true,
"noEmit": true,
"strict": true,
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react",
"experimentalDecorators": true, // for sequelize-typescript
"emitDecoratorMetadata": true // for sequelize-typescript
},
"include": [
"./src/"
]
}Sequelize-typesctipt setup:
import config from ...;
import {Sequelize} from 'sequelize-typescript';
const sequelize = new Sequelize({
database: config.db.database,
username: config.db.username,
password: config.db.password,
host: config.db.host,
port: config.db.port,
dialect: "mysql",
pool: {
max: 100,
min: 0,
idle: 1000000
},
define: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
timestamps: true
},
logging: config.sequelize_logging ? (x: any) => console.log("[SEQUELIZE]",x) : () => false,
typeValidation:false,
modelPaths: [__dirname + "/../entities"]
});
sequelize
.authenticate()
.catch(err => {
console.error('Unable to connect to the database:', err);
});
sequelize.sync({
force: false
});
export default sequelize;insert short code snippets here
Activity
RobinBuschmann commentedon May 3, 2019
Hey @BjarkeNL, I think the issue is indeed caused by this behaviour of babel. I've got the impression that
_definePropertyoverrides the previously defined getters and setters created by sequelize. As you already mentioned thedataValuesare updated. But when callinguser.firstNameit uses the getter which is defined by the babel plugin within the constructor ofUserand in turn doesn't get through thedateValues. So to me it definitely looks like an issue withbabel-plugin-proposal-class-properties. Can you omit this plugin? If not, I'm trying to think about a workaround.Hope this helps!
Telokis commentedon May 11, 2019
@RobinBuschmann Hey! I have this exact problem. Did you manage to find a solution?
loganfsmyth commentedon May 12, 2019
This is a current difference in behavior between Typescript and ECMAScript class fields. Babel's TypeScript transformation parses TS, but the runtime behavior for ECMA features follows the ECMA spec, not TS.
microsoft/TypeScript#27644
RobinBuschmann commentedon May 13, 2019
@loganfsmyth thanks for giving more insights. So
babel-plugin-proposal-class-propertiesis doing it the right way 🤔@Telokis @BjarkeNL
For now I can only provide a workaround (Overriding the defined properties again):
DalerAkhmetov commentedon Feb 7, 2020
@RobinBuschmann, this solution doesn't work with associations.
example.someModel is undefined where example is instance of Example.
RobinBuschmann commentedon Feb 8, 2020
@DalerAkhmetov Good point. One need to add the association keys as well like so:
(Untested, but) Hope this helps for now!
DalerAkhmetov commentedon Feb 9, 2020
thanks, it works
DalerAkhmetov commentedon Jul 17, 2020
Hi @RobinBuschmann, I found a bug. Solution #612 (comment) doesn't work if I use bulkCreate and pass records as array of Model instance
For example:
I don't know how to fix it. Do you have any ideas?
tommoor commentedon Dec 28, 2021
This is awfully close but it does seem to result in any modified associations being included in the models internal
changed()calculation. for example if you have aBelongsToassociation setup forcollection, and then do something like:The query that's generated is invalid because it tries to save the
collectionfield. I don't yet have a solution, suspect I need to understand sequelize internals much more fully.Edit: This solution also overrides any existing custom getter/setters 😢
Edit 2: You can find a working implementation of the
Fixdecorator here: https://github.com/outline/outline/blob/0de6650aa5e97612d46b459de719a000da6349be/server/models/decorators/Fix.ts#L9EdisonJpp commentedon Oct 18, 2023
I'm getting the same error and I've not fixed it yet. I tried all the above solutions and it does not work for me :'(