sequelize / sequelize/sequelize

fail if sourceKey for hasMany was not found

Open
#7,683 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: bug
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

What you are doing?

const hotelAttributeList = sequelize.define('hotelAttributeList', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'hotel_attribute_list_id'
    },
    eanHotelId: {
      type: DataTypes.INTEGER(11),
      allowNull: true,
      field: 'ean_hotel_id'
    }
},
{
    tableName: 'hotel_attribute_list'
})
const hotelProperties = sequelize.define('hotelProperties', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'hotel_properties_id'
    },
    eanHotelId: {
      type: DataTypes.STRING(10),
      allowNull: false,
      field: 'ean_hotel_id'
    }
},
{
    tableName: 'hotel_properties'
});
hotelProperties.hasMany(hotelAttributeList, {
  sourceKey: 'eanHotelId',
  foreignKey: 'eanHotelId'
});

What do you expect to happen?

I expect the column ean_hotel_id to join to ean_hotel_id between the tables

What is actually happening?

primary key hotel_properties_id is joining against ean_hotel_id.

Dialect: mysql
Sequelize version: 3.30.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the hasMany association from the issue body with the MySQL dialect, then trace how Sequelize resolves sourceKey and foreignKey for this association. Done means the generated join uses hotel_properties.ean_hotel_id and hotel_attribute_list.ean_hotel_id rather than the primary key.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql, node.js
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.