gpt4 book ai didi

node.js - 表示链接两个模型的关联表

转载 作者:行者123 更新时间:2023-12-03 22:37:44 25 4
gpt4 key购买 nike

我是 NodeJs 领域的新手,我正在将 Sequelize 与 PostgreSQL 用于 ExpressJs 项目。
我在寻找表示两个模型之间关联表的正确方法时遇到了一些问题。
这是我的两个表:

“玩家”:

  • id
  • 名字
  • 姓氏
  • year_of_birth
  • 技能
  • 登录
  • 密码
  • created_at
  • 已更新_at

  • “团队”:
  • id
  • 名称
  • created_at
  • 已更新_at

  • 以及关联表“team_players”:
  • id
  • id_team
  • id_player

  • 鉴于互联网上的一些示例,我尝试了以下语法:
    Player.belongsToMany(Team, {foreignKey: 'id_player', through: 'team_players'});


    当我尝试通过这样的关联访问数据时:
    Player.findAll({ include :[{model: Team,foreignKey: 'id_team', through: 'team_players'}] })
    . then(results)[...]

    它不工作。

    这是我所做的模型定义。

    播放器模型定义:
    const { Sequelize } = require('../config/postgres');
    const { sequelizeInstance } = require('../config/postgres');

    const { Model } = Sequelize;
    const Team = require('./Team');

    class Player extends Model {}
    Player.init({
    firstname: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {isAlpha: true}
    },
    lastname: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {isAlpha: true}
    },
    year_of_birth: {
    type: Sequelize.INTEGER,
    allowNull: false,
    validate: {
    min: 1950,
    max: 2019,
    isNumeric: true
    },
    },
    skills: {
    type: Sequelize.INTEGER,
    allowNull: false,
    validate: {isInt: true}
    },
    login: {
    type: Sequelize.STRING,
    allowNull: false,
    validate:
    {
    isAlphanumeric: true,
    unique: true
    }
    },
    password: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {isAlphanumeric: true}
    },
    },
    {
    sequelize: sequelizeInstance,
    timestamps: true,
    modelName: 'player',
    tableName: 'players'
    });

    Player.belongsToMany(Team, {foreignKey: 'id_player', through: 'team_players'});

    module.exports = Player;


    团队模型定义:
    const { Sequelize } = require('../config/postgres');
    const { sequelizeInstance } = require('../config/postgres');

    const { Model } = Sequelize;
    const Player = require('./Player');

    class Team extends Model {}
    Team.init({
    name: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true
    },
    },
    {
    sequelize: sequelizeInstance,
    timestamps: true,
    modelName: 'team',
    tableName: 'teams'
    });

    module.exports = Team;

    这是错误消息:
      name: 'SequelizeDatabaseError',
    parent:
    { error: column teams->team_players.teamId does not exist
    at Connection.parseE (/home/nku/sources/.perso/pasanga/server/node_modules/pg/lib/connection.js:602:11)
    at Connection.parseMessage (/home/nku/sources/.perso/pasanga/server/node_modules/pg/lib/connection.js:399:19)
    at Socket.<anonymous> (/home/nku/sources/.perso/pasanga/server/node_modules/pg/lib/connection.js:121:22)
    at Socket.emit (events.js:197:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:150:17)
    name: 'error',
    length: 125,
    severity: 'ERROR',
    code: '42703',
    detail: undefined,
    hint: undefined,
    position: '776',
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'parse_relation.c',
    line: '3293',
    routine: 'errorMissingColumn',
    sql:
    'SELECT "player"."id", "player"."firstname", "player"."lastname", "player"."yob", "player"."skills", "player"."login", "player"."password", "player"."created_at", "player"."updated_at", "teams"."id" AS "teams.id", "teams"."name" AS "teams.name", "teams"."created_at" AS "teams.created_at", "teams"."updated_at" AS "teams.updated_at", "teams->team_players"."created_at" AS "teams.team_players.created_at", "teams->team_players"."updated_at" AS "teams.team_players.updated_at", "teams->team_players"."id_player" AS "teams.team_players.id_player", "teams->team_players"."teamId" AS "teams.team_players.teamId" FROM "players" AS "player" LEFT OUTER JOIN ( "team_players" AS "teams->team_players" INNER JOIN "teams" AS "teams" ON "teams"."id" = "teams->team_players"."teamId") ON "player"."id" = "teams->team_players"."id_player";' }

    最佳答案

    您从球员到球队的关联是可以的。我认为您可以通过将 team_players.id_team 重命名为默认名称 team_players.teamId 来解决问题

    或者,您可以添加从 Team-to-Players 的关联以处理您选择的外键,如下所示:
    Team.belongsToMany (Player,{foreignKey: 'id_team', through: 'team_players'});
    6 月 5 日编辑

    这是一些工作代码:

    const Sequelize = require('sequelize');
    const node_app_name = require("../sqltest"); // my db instance here
    const { Model } = Sequelize;

    class Player extends Model {}
    Player.init({
    firstname: Sequelize.STRING,
    lastname: Sequelize.STRING,
    },
    {sequelize: node_app_name.sequelizeDB});

    class Team extends Model {}
    Team.init({
    name: Sequelize.STRING
    },
    {sequelize: node_app_name.sequelizeDB});

    class TeamPlayer extends Model {}
    TeamPlayer.init({
    id_team: Sequelize.INTEGER,
    id_player: Sequelize.INTEGER,
    },
    {sequelize: node_app_name.sequelizeDB});

    Player.belongsToMany(Team, {foreignKey: 'id_player', through: TeamPlayer});
    Team.belongsToMany (Player,{foreignKey: 'id_team', through: TeamPlayer});

    Player.findAll({
    include:
    [{
    model: Team,
    required: false
    }]
    }).then(pat => {
    console.log(pat);
    });

    希望这可以帮助。请注意, belongsToMany() 关联发生在 之后 两个模型都被初始化。

    关于node.js - 表示链接两个模型的关联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56441123/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com