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.

Allow foreignKey field name and column name definition

See original GitHub issue

What 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:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ssljiviccommented, Jun 13, 2016

It should be mapped like this:

sequelize.define('Image', {
    id: {
        field: 'image_id',
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    //...
    imageTypeId: {
        field: 'image_type_id',
        type: DataTypes.BIGINT
    }
}, {
    tableName: 'image',
    classMethods: {
        associate: function (models) {
            models.Image.belongsTo(models.ImageType, {
                foreignKey: {
                    fieldName: 'imageTypeId'
                },
                as: 'imageType'
            });
        }
    }
});

Thanks @mickhansen .

1reaction
weldermarcosxdcommented, Jul 3, 2017

@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.

Read more comments on GitHub >

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

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