Skip to content

hasMany relationship with sourceKey specified does not seem to work. defaults to primary key of source table #7255

Closed

Description

@stringbeans

What you are doing?

I'm trying to create a hasMany relationship between Country and TranslationEntry. Here is my code below:

sequelize.define('Country', {
    countryId: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'country_id'
    },
    code: {
      type: DataTypes.CHAR(2),
      allowNull: false,
      defaultValue: '',
      field: 'code'
    },
    nameTranslationId: {
      type: DataTypes.INTEGER(11),
      allowNull: true,
      field: 'name_translation_id'
    }
  })

sequelize.define('TranslationEntry', {
    translationId: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      field: 'translation_id'
    },
    languageId: {
      type: DataTypes.INTEGER(3),
      allowNull: false,
      primaryKey: true,
      references: {
        model: 'language',
        key: 'language_id'
      },
      field: 'language_id'
    },
    translation: {
      type: DataTypes.TEXT,
      allowNull: true,
      field: 'translation'
    }
})

models.Country.hasMany(models.TranslationEntry, {
  as: 'nameTranslationEntries',
  sourceKey: 'nameTranslationId',
  foreignKey: 'translationId'
})

And I'm querying by doing the following:

Country.find({
            where: {
              code: 'ca'
            },
            include: [{
              model: this.DataAccessor.models.TranslationEntry,
              as: 'nameTranslationEntries'
            }]
          })

What do you expect to happen?

I would expect the generated query to join on Country.name_translation_id = TranslationEntry.translation_id

What is actually happening?

Viewing the query logs, I am seeing it is attempting to join based on Country.country_id = TranslationEntry.translation_id

Dialect: mariadb
Database version: mysql Ver 15.1 Distrib 10.0.24-MariaDB, for osx10.11 (x86_64) using readline 5.1
Sequelize version: 3.30.2

Activity

  1. sushantdhiman commented on Feb 19, 2017

    @sushantdhiman
    Contributor

    Can you try master as well

  2. devsign-consulting commented on May 18, 2017

    @devsign-consulting

    I'm having the same issue. any ideas?

  3. stale commented on Jul 18, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  4. johnny-dash commented on Sep 7, 2017

    @johnny-dash

    I have the same issue, even I define the sourceKey, it still refer the primary key in source object

  5. sushantdhiman commented on Sep 7, 2017

    @sushantdhiman
    Contributor

    With master ?

  6. nmehta01 commented on Nov 6, 2017

    @nmehta01

    I am having the same issue with v4.22.5

  7. added
    type: bug
    and removed on Nov 8, 2017
  8. nmehta01 commented on Nov 8, 2017

    @nmehta01

    To add a bit more info to this thread - this seems to only be an issue when the attribute name is different than the underlying column name(field)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions