简体   繁体   English

Postgres:SequelizeDatabseError:列 ID 不存在

[英]Postgres: SequelizeDatabseError: column id does not exist

I am using Postgresql with code-first approach in my Node.JS project.我在我的 Node.JS 项目中使用代码优先方法的 Postgresql。 Some of the tables were already there in the inherited code.一些表已经存在于继承的代码中。 There is a table called user_games storing the information about which user is playing which games.有一个名为 user_games 的表存储有关哪个用户正在玩哪些游戏的信息。 Model of the table has been defined as follows:表的Model已经定义如下:

'use strict';
const {
  Model
} = require( 'sequelize' );
module.exports = ( sequelize, DataTypes ) => {
  class UserGames extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate( models ) {
      // define association here
      UserGames.belongsTo( models.Game, {
        as: 'Games',
        foreignKey: 'game_id',
        onDelete: 'NO ACTION',
        onUpdate: 'NO ACTION'
      } );

      UserGames.belongsTo( models.User, {
        as: 'User',
        foreignKey: 'user_id',
        onDelete: 'NO ACTION',
        onUpdate: 'NO ACTION'
      } );

      UserGames.hasMany( models.GameUrl, {
        foreignKey: 'game_id'
      } );
    }
  }

  UserGames.init( {
    user_game_id: { // eslint-disable-line camelcase
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      allowNull: false
    },
    user_id: DataTypes.STRING, // eslint-disable-line camelcase
    games_gamer_id: DataTypes.STRING, // eslint-disable-line camelcase
    game_id: DataTypes.UUID // eslint-disable-line camelcase
  }, {
    sequelize,
    createdAt: 'createdAt',
    updatedAt: 'updatedAt',
    modelName: 'UserGames',
    tableName: 'user_games',
  } );
  return UserGames;
};

Things were working fine but suddenly the following (and other related) code started complaining "column UserGames.id does not exist":一切正常,但突然以下(和其他相关)代码开始抱怨“列 UserGames.id 不存在”:

models.Game.findAndCountAll( {
include: [ {
  model: models.UserGames,
  as: 'UserGames',
  where: { user_id: userId }, // eslint-disable-line camelcase
} ],
attributes: [ 'name', 'description_text', 'icon_url' ],
order: [
  [ 'name', 'ASC' ]
],

} ); });

So, we had to modify 'include' part for the table user_games to specify the attributes explicitly as below:因此,我们必须修改表 user_games 的“include”部分以明确指定属性,如下所示:

include: [ {
  model: models.UserGames,
  as: 'UserGames',
  where: { user_id: userId }, // eslint-disable-line camelcase
  attributes: [ 'game_id' ], //otherwise it will look for 'id' field also.
} ],

As shown in the model definition, the table contains a column user_game_id as the primary key and as I mentioned, things were working fine a few days ago.如 model 定义所示,该表包含一个列 user_game_id 作为主键,正如我提到的,几天前一切正常。 So, I am not able to understand the reason for the error.所以,我无法理解错误的原因。 Any suggestion will be highly appreciated.任何建议将不胜感激。

Edit: Here is the game model:编辑:这是游戏 model:

/* eslint-disable camelcase */
'use strict';
const {
  Model
} = require( 'sequelize' );
module.exports = ( sequelize, DataTypes ) => {
  class Game extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate( models ) {
      // define association here
      Game.belongsTo( models.Partner, {
        as: 'Partner',
        foreignKey: 'partner_id',
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE'
      } );
      Game.hasMany( models.UserGames, {
        as: 'UserGames',
        foreignKey: 'game_id',
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE'
      } );
      Game.hasMany( models.GameAsset, {
        as: 'GameAssetDetails',
        foreignKey: 'game_id',
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE'
      } );
      Game.hasMany( models.GameUrl, {
        as: 'GameUrlDetails',
        foreignKey: 'game_id',
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE'
      } );
      Game.hasMany( models.GameTestingComment, {
        foreignKey: 'game_id',
        onDelete: 'CASCADE',
        onUpdate: 'CASCADE'
      } );
      Game.hasMany( models.OfferSchedule, {
        foreignKey: 'game_id'
      } );
    }
  }

  Game.init( {
    game_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      allowNull: false
    },
    partner_id: {
      type: DataTypes.UUID,
      allowNull: false
    },
    name: DataTypes.STRING,
    description: DataTypes.TEXT,
    description_text: DataTypes.TEXT,
    icon_url: DataTypes.STRING,
    status: {
      type: DataTypes.ENUM,
      values: [ "1", "2", "3", "4" ],
      defaultValue: "1",
      comment: "1 => 'In Development', 2 => 'Submit for Approval', 3 => 'Approved', 4 => 'Rejected'",
    },
    approved_date: {
      allowNull: true,
      type: DataTypes.DATE
    },
    submitted_date: {
      allowNull: true,
      type: DataTypes.DATE
    },
    is_active: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      allowNull: false
    }
  }, {
    sequelize,
    createdAt: 'createdAt',
    updatedAt: 'updatedAt',
    modelName: 'Game',
    tableName: 'game',
  } );
  return Game;
};

Ultimately, we are able to figure out the source of issue.最终,我们能够找出问题的根源。 A few days back, one of the developers in the team added the wrong association.几天前,团队中的一位开发人员添加了错误的关联。 He set 'id' column of UserGames as foreign key in other table though the UserGames did not have 'id' column at all.他将 UserGames 的“id”列设置为其他表中的外键,尽管 UserGames 根本没有“id”列。 Below is the faulty code:下面是错误的代码:

static associate( models ) {
  GameProducts.hasMany( models.UserGames, {
    as: 'SubscriptionPlans',
    sourceKey: "game_product_id",
    foreignKey: 'id' //Faulty code. UserGames does not have 'id' column
  } );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM