question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Doesn't create foreign key when defining association

See original GitHub issue

Referencing 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:

  1. Define models UserModel, UserType.
  2. Run the following
  3. 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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
Qarun-Qadir-Bissoondialcommented, May 16, 2020

Can confirm, I’m having this same problem using PostgreSQL

3reactions
hugo1707commented, Jun 11, 2019

same problem

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found