Postgres:SequelizeDatabseError:列 ID 不存在

发布于 2025-01-14 19:26:52 字数 4579 浏览 3 评论 0原文

我在 Node.JS 项目中使用 Postgresql 和代码优先方法。一些表已经存在于继承的代码中。有一个名为 user_games 的表,存储有关哪个用户正在玩哪些游戏的信息。表的模型定义如下:

'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;
};

一切工作正常,但突然以下(和其他相关)代码开始抱怨“列 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' ]
],

} );

因此,我们必须修改表 user_games 的“包含”部分,以显式指定属性,如下所示:

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.
} ],

如模型定义所示,该表包含列 user_game_id 作为主键,正如我所提到的,事情运行良好几天前。所以,我无法理解错误的原因。任何建议将受到高度赞赏。

编辑:这是游戏模型:

/* 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;
};

I am using Postgresql with code-first approach in my Node.JS project. 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. Model of the table has been defined as follows:

'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" :

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:

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. So, I am not able to understand the reason for the error. Any suggestion will be highly appreciated.

Edit: Here is the game 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;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2025-01-21 19:26:52

最终,我们能够找出问题的根源。几天前,团队中的一位开发人员添加了错误的关联。他将 UserGames 的“id”列设置为其他表中的外键,尽管 UserGames 根本没有“id”列。下面是错误代码:

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

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. 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
  } );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文