sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

Targetkey is not automatically generated in associations

Open koswarabilly opened this issue 1 year ago • 0 comments

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?

koswarabilly avatar Jul 30 '24 02:07 koswarabilly