Doesn't create foreign key when defining association
See original GitHub issueReferencing from an old #2031 , hasOne
association does not creating the foreign key, as it intend to do according to hasOne Documentation .
What are you doing?
I was creating an UserModel
and UserStatus
model. Status like waiting for approval
, approved
, rejected
etc. Association i meant to create like, Every user should have a status. So i put code like this
// UserModel is created using `sequelize.define`
// UserType is created using `sequelize.define`
UserModel.hasOne(UserType, { foreignKey: { allowNull : false }, onDelete: "CASCADE" })
To Reproduce Steps to reproduce the behavior:
- Define models UserModel, UserType.
- Run the following
- See error
What do you expect to happen?
i expect to create a foreign key userTypeId
to be created inside the users
table.
What is actually happening?
No foreign key association was created in the user
table
CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `firstName` VARCHAR(30) NOT NULL, `lastName` VARCHAR(30) NOT NULL, `email` VARCHAR(30) NOT NULL UNIQUE, `password` VARCHAR(255) NOT NULL);
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any
Dialect library version: 4.0.8 Database version: sqlite3 Sequelize version: 5.8.8 Node Version: 8.10.0 OS: Ubuntu 18.04
Tested with latest release:
- No
- Yes, specify that version:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:12 (4 by maintainers)
Top Results From Across the Web
doesn't create foreign-key automatically when defining ...
It works, but the foreign key is created by manual. According to the document, belongsTo(), hasMany() or hasOne() should create foreign key ......
Read more >Sequelize associations not generating foreign key
I've checked docs for setting foreign keys on models, and it doesnt really mention if say or where i set an option to,...
Read more >3 common foreign key mistakes (and how to avoid them)
Dangling foreign keys 3. Not creating foreign key indexes Bonus: Not using foreign keys Key takeaway: think before you CREATE TABLE.
Read more >Hibernate Tips: How to model an association that doesn't ...
Some table models use non-primary key columns as foreign keys. You can map these associations with an additional annotation with JPA and Hibernate....
Read more >Foreign keys and associations - GitLab Docs
Every foreign key must define an ON DELETE clause, and in 99% of the cases this should be set to CASCADE . Indexes....
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
Can confirm, I’m having this same problem using PostgreSQL
same problem