Repository navigation
Description
- Is missing
await sequelize.sync()beforeasync function doStuffWithUser() { - Is missing to invoke
doStuffWithUserfunction. - I also get a warning:
(sequelize) Warning: PostgresSQL does not support 'INTEGER' with LENGTH, UNSIGNED or ZEROFILL. Plain 'INTEGER' will be used instead - Nothing is saved to the database: I get this error
ValidationError [SequelizeValidationError]: notNull Violation: User.name cannot be null
After a long time figuring out what I had wrong in my code, I figured out why attributes are not saved to the db:
Let's say I copy the UserModel from the guide: (simplified)
class User extends Model<UserAttributes, UserCreationAttributes>
implements UserAttributes {
public id!: number; // Note that the `null assertion` `!` is required in strict mode.
public name!: string;
public preferredName!: string | null; // for nullable fields
// timestamps!
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
This will not save any attributes in the db. If I comment out the public name!: string; I get: INSERT INTO "users" ("name","createdAt","updatedAt") VALUES ($1,$2,$3) RETURNING "id","name","preferredName","createdAt","updatedAt"; (note that there is no preferredName)
Notice how preferredName is null. That's because is in the model!
Now If I comment both attributes: INSERT INTO "users" ("name","preferredName","createdAt","updatedAt") VALUES ($1,$2,$3,$4) RETURNING "id","name","preferredName","createdAt","updatedAt";
class User extends Model<UserAttributes, UserCreationAttributes>
implements UserAttributes {
public id!: number; // Note that the `null assertion` `!` is required in strict mode.
// public name!: string;
// public preferredName!: string | null; // for nullable fields
// timestamps!
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
}
I get all keys!
As you can see this is unusable and I'm not even sure how to use it now with typescript.
I debugged a bit and seems like it applies correctly all attributes on a first pass, but somehow there is another pass and sets all attributes to null. I couldn't figure out who invoked the 2nd call to set to null (code a bit too dynamic).
Code to start sequelize instance
this.sequelize = new Sequelize(
databaseConfig.name,
databaseConfig.username,
databaseConfig.password,
{
host: databaseConfig.host,
port: databaseConfig.port,
dialect: 'postgres',
pool: {
max: 15,
min: 1,
acquire: 30000,
idle: 10000,
},
logging: true,
}
)
await this.sequelize.authenticate();
Issue Description
What are you doing?
Here is the link to the SSCCE for this issue: LINK-HERE
// You can delete this code block if you have included a link to your SSCCE above!
// MINIMAL, SELF-CONTAINED code here (SSCCE/MCVE/reprex)What do you expect to happen?
I wanted Foo!
What is actually happening?
The output was Bar!
Output here
Additional context
Add any other context or screenshots about the feature request here.
Environment
- Sequelize version: XXX
- Node.js version: XXX
- Operating System: XXX
- If TypeScript related: TypeScript version: XXX
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 PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
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.


Activity
[-]Typescript tutorial completely broken `"sequelize": "^6.3.4",`[/-][+]Typescript unusable & Typescript tutorial completely broken `"sequelize": "^6.3.4",`[/+]github-actions commentedon Oct 31, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
ilijapuaca commentedon Nov 8, 2021
This prohibits usage of sequelize with TS which is highlighted by the TS docs example not working. Not sure if this is tracked in another ticket but if not it should stay open?
WikiRik commentedon Nov 9, 2021
@ilijapuaca I am also not sure if this is tracked in another ticket, but I'll reopen it just to be sure. Thanks for the reply!
github-actions commentedon Nov 24, 2021
This 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. 🙂
ilijapuaca commentedon Nov 24, 2021
Still relevant, not stale
knoid commentedon Dec 29, 2021
Same problem here, this is happening since typescript v4.3.2. I've read the release notes but nothing stands out to me.
ephys commentedon Dec 29, 2021
The fields disappearing looks like a duplicate of #10579 (with potential solution #10579 (comment))
The rest I'm unsure about and can't help without a SSCCE or a repo we can look at and find what's wrong
knoid commentedon Dec 29, 2021
@ephys You can find an example here: https://github.com/knoid/sequelize-test-12603
All I did was a typescript project where index.ts is a copy of ModelInit.ts.
ephys commentedon Dec 29, 2021
Thanks, took at look at it :)
So the warning
PostgresSQL does not support 'INTEGER' with LENGTH, UNSIGNED or ZEROFILL. Plain 'INTEGER' will be used insteadis correct. The example uses mysql which supports unsigned integers while your example uses postgres, which does not. It's just a warning and removing.UNSIGNEDremoves the warning.Not specifying
await sequelize.sync()is not incorrect. You should avoid it in production for instance, and it's not relevant to the typescript example. It's also normal that it doesn't invokedoStuffWithUser. It's only a typing example.I'm going to work on the TS docs a bit so I'll include these anyway so the example runs.
tried launching using
npm startand it worked, so looks like it works usingtsc&ts-node. #10579 is still just a babel issue.I'm keeping the issue open as a documentation issue. I'll tackle it along with #10579
knoid commentedon Dec 29, 2021
ok, I may have skipped some explaining. This is the output I get by running
npm start.The line that says
undefined undefined undefinedis line 218 where it's trying to accessnewUser's properties and it just gets undefined for every one. It then fails on the following line becausecreateProjectis also undefined and it's trying to invoke it.To make this all work, I previously created a postgres database called
sequelize-testas you can see from the credentials on line 19.Thanks for everything, I hope this clarifies the error we are getting.
ephys commentedon Dec 29, 2021
Hmm sorry I missed something I changed during my tests.
Set
"target": "esnext",to"target": "ES2021",in tsconfig.jsonIf you're going to use
esnext, adddeclarein front of the fields you're only declaring for typing:I'm going to add all that to the documentation
knoid commentedon Dec 29, 2021
That's interesting! You can see my latest commit with the appropriate changes. I didn't know about this
declarekeyword. Now it all works. Thanks!ephys commentedon Jan 2, 2022
Closed by #13877