Why is my foreign key column returned twice after sending a POST request with Sequelize and Postgres?

Issue

I have a record (= album) model and an artist model and I am trying to POST a record for an artist via Postman to this URL: http://localhost:3000/api/artists/:artistId/records. Postgres returns the error errorMissingColumn and Perhaps you meant to reference the column \"VBRecords.artistId\". (VBRecords is what I called the table to avoid possible complications with internal keywords.)

My server logs the following query, which is executed on above POST request:

Executing (default): INSERT INTO "VBRecords" ("id","title","createdAt","updatedAt","artistId") VALUES (DEFAULT,$1,$2,$3,$4) RETURNING "id","title","createdAt","updatedAt","ArtistId","artistId";

I believe the ArtistId (second to last argument in the RETURNING block) is the one causing the problem, but I don’t know where it’s coming from. As far as I understand the documentation, it should not be there, because the last argument artistId is the right one as it’s coming from the params.

These are my models. I’m new to Sequelize, maybe there’s something wrong with the model definitions.

models/artist.js

const { Model } = require('sequelize')

module.exports = (sequelize, DataTypes) => {
  class Artist extends Model {
    static associate(models) {
      // removing this association (see UPDATE at the bottom of this post)
      // Artist.hasMany(models.Record, { as: 'records' })
    }
  }

  Artist.init({
    name: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    modelName: 'Artist',
    sequelize,
    tableName: 'VBArtists'
  })

  return Artist
}

models/record.js

const { Model } = require('sequelize')

module.exports = (sequelize, DataTypes) => {
  class Record extends Model {
    static associate(models) {
      Record.belongsTo(models.Artist, {
        foreignKey: 'artistId',
        onDelete: 'CASCADE'
      })
    }
  }

  Record.init({
    title: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    modelName: 'Record',
    sequelize,
    tableName: 'VBRecords'
  })

  return Record
}

Any help is appreciated and if I can add more code, please let me know which files.

Thanks in advance!

UPDATE:

After removing the hasMany association from the artist model, it works. But I don’t understand why, as even the sequelize docs say to add both associations to a one-to-many relationship:

Implementation

The main way to do this is as follows:

Team.hasMany(Player); Player.belongsTo(Team);

Solution

I fixed it by using those associations inside the models:

artist.js

const { Model} = require('sequelize')

module.exports = (sequelize, DataTypes) => {
  class Artist extends Model {
    static associate(models) {
      Artist.hasMany(models.Record, {
        foreignKey: 'artistId',
        onDelete: 'CASCADE'
      })
    }
  }

  ...
}

record.js

const { Model } = require('sequelize')

module.exports = (sequelize, DataTypes) => {
  class Record extends Model {
    static associate(models) {
      Record.belongsTo(models.Artist, {
        foreignKey: 'artistId'
      })
    }
  }

  ...
}

I guess the trick was to add the foreign key attribute to both associations. Hopefully this will help someone in the future.

Answered By – J. Unkrass

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published