Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Targetkey is not automatically generated in associations #666

Open
koswarabilly opened this issue Jul 30, 2024 · 0 comments
Open

Targetkey is not automatically generated in associations #666

koswarabilly opened this issue Jul 30, 2024 · 0 comments

Comments

@koswarabilly
Copy link

Context

Lets say I have two tables countries and users. In countries there are columns like id, name, code. code is a unique column and an alpha-2 of the country (UK, US, etc). users is linked to countries via code.

What is generated

In users model

countryCode: {
          type: DataTypes.STRING(2),
          allowNull: false,
          references: {
            model: 'countries',
            key: 'code',
          },
          field: 'country_code',
},

In associations

users.belongsTo(countries, { as: 'countryCodeCountry', foreignKey: 'countryCode' });
countries.hasMany(users, { as: 'users', foreignKey: 'countryCode' });

With this associations, the Sequelize generate wrong query resulting in error of trying to compare character varying to bigint

What is expected to generate

users.belongsTo(countries, { as: 'countryCodeCountry', foreignKey: 'countryCode', targetKey: 'symbol' });
countries.hasMany(users, { as: 'users', foreignKey: 'countryCode' });

Am I missing configuration or I am doing it wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant