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.

Foreign key is being capitalised in query resulting in "SequelizeDatabaseError: column Book.UserId does not exist"

See original GitHub issue

What you are doing?

I am trying to perform a simple join;

models.User.find({
      where: {
        emailAddress: request.payload.emailAddress
      },
      include: [{
        model: models.Book,
      }]
    })

What is actually happening?

and I get the error ‘“SequelizeDatabaseError: column Book.UserId does not exist”’

The field is called ‘userId’. How can I fix this?

Dialect: postgres __Database version: 9.4 __Sequelize version: 3.20.0

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

23reactions
janmeiercommented, May 2, 2016

(Didn’t you post your complete models before 😉?)

The foreign key name will match the name of the model - so if the user has User as the name, the foreign key will be UserId. For example if you defined

User.hasOne(models.Book, { foreignKey: 'userId' });
Book.belongsTo(User)

Sequelize will get confused and add both userId and UserId - You have to add the foreign key to both associations.

Without seeing your complete model definitions and associations so I can replicate the issue, its hard to say whats at play here 😉

22reactions
papbcommented, Jul 15, 2019

@fiftin You should explicitly specify the foreign key on both ends:

User.hasOne(models.Book, { foreignKey: 'userId' });
Book.belongsTo(User, { foreignKey: 'userId' });

Or not specify it at all.

Are you looking for a different solution? I don’t think that exists.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Column Questions.UserId doesn't exist in associated model
Strictly speaking these two associations are independent and they know nothing about each other options like foreignKey .
Read more >
Postgres : Relation does not exist error - DBA Stack Exchange
But when I try to fire a select * query, it gave me this error: dump=> select * from Approvals; ERROR: relation "approvals"...
Read more >
pwning-owasp-juice-shop - StudyLib
The content of this book was written for v9.0.1 of OWASP Juice Shop. ... As long as the flag code key is identical...
Read more >
Sequelize - How to fix relation does not exist error
When you're running Sequelize code to fetch or manipulate data from a PostgreSQL database, you might encounter an error saying relation <table ...
Read more >
sequelize-typescript - npm
There are 544 other projects in the npm registry using ... class BookAuthor extends Model { @ForeignKey(() => Book) @Column bookId: number; ...
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