Foreign key is being capitalised in query resulting in "SequelizeDatabaseError: column Book.UserId does not exist"
See original GitHub issueWhat 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:
- Created 7 years ago
- Reactions:1
- Comments:11 (4 by maintainers)
Top 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 >
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
(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 beUserId
. For example if you definedSequelize will get confused and add both
userId
andUserId
- 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 😉
@fiftin You should explicitly specify the foreign key on both ends:
Or not specify it at all.
Are you looking for a different solution? I don’t think that exists.