Allow foreignKey field name and column name definition
See original GitHub issueWhat you are doing?
When association is defined like below sequelize will expect column with name ‘image_type_id’ to exists in the ‘image’ table and will add a field in the Image model instance with name ‘image_type_id’.
Image.belongsTo(ImageType, {
foreignKey: {
name: 'image_type_id'
},
as: 'imageType'
});
What do you expect to happen?
I would like to be able to separately define model filed name and table column name, so that I do not have to use snake case column names in sequelize queries.
What is actually happening?
The name of the model field is ‘image_type_id’ instead of ‘imageTypeId’.
Something like would be ok:
Image.belongsTo(ImageType, {
foreignKey: {
name: 'image_type_id',
fieldName: 'imageTypeId'
},
as: 'imageType'
});
Dialect: postgres Database version: 9.4.4 Sequelize version: 3.23.3
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >Database column naming for foreign key - Stack Overflow
The short answer is no - don't put "FK" in column names of foreign key columns. You can still signal the intent of...
Read more >SQL Foreign key - SQLShack
A Foreign key is constraint that enforces referential integrity in SQL server database. It uses a column or combination of columns that is...
Read more >Primary and Foreign Key Constraints - SQL Server
In a foreign key reference, a link is created between two tables when the column or columns that hold the primary key value...
Read more >ForeignKey/OneToOneField attribute names should be valid ...
Based on the principle that defining things like foreign keys should only add functionality (and not remove any) it seems quite important that...
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
It should be mapped like this:
Thanks @mickhansen .
@ssljivic how that association would be defined in ImageType side, Im getting ‘model is not associated with model2’ no matter hard I try, Thank you.