Marc
Marc

Reputation: 43

Sequelize for NodeJS and hasMany using UUID error

I have a model where 'Matches' has many 'MatchRoundTypes'

Matches has a primary key of uuid matchRoundTypes references Matches with matchesUUID as its column name.

When i try to do a find on Matches that includes MatchRoundTypes i get:

DEBUG: Error: MatchRoundTypes is not associated to Matches!

my query looks like:

Matches.findAll({where: ["isPublished = ?", true], include: [MatchRoundTypes]})

I have issued the following command prior to all this:

Matches.hasMany(MatchRoundTypes, { as: 'roundMaps', foreignKey: 'matchUUID', useJunctionTable: false})

I have tried numerous variations on the hasMany statement ... including the foreignKey and not, etc.

Here is my Matches model:

sequelize.define('Matches', {
        uuid:    {type: Seq.STRING(36),  allowNull: false, primaryKey: true},
        name:    {type: Seq.STRING(64),  allowNull: false},

    }, {tableName: 'match', freezeTableName: true})

Here is my MatchRoundTypes model:

sequelize.define('MatchRoundTypes', {
{
        uuid:               {type: Seq.STRING(36),  allowNull: false, primaryKey: true},
        roundTypeUUID:      {type: Seq.STRING(36),  allowNull: false},
        roundName:          {type: Seq.STRING(64),  allowNull: true},
        matchUUID: {
          type: Seq.STRING(36),
          references: "match",
          referencesKey: "uuid",
          allowNull: false
        }

    }, {tableName: 'matchRoundTypes', freezeTableName: true})

Any insights are most welcome.

Upvotes: 1

Views: 1242

Answers (1)

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28788

You are using an alias when defining the hasMany association, therefore you also have to tell sequelize to use that alias when doing eager loading:

Matches.findAll({
  where: ["isPublished = ?", true], 
  include: [{ model: MatchRoundTypes, as: 'roundMaps' }]
})

Upvotes: 1

Related Questions