繁体   English   中英

我与 sequelize 和 postgres 的关联有问题

[英]I have a trouble with association with sequelize and postgres

我定义了两张表,一张用于电影,一张用于角色,它们通过 MovieCharacter 表相互连接。 通过belongstomany 将表设置为维护一对多关系允许我创建重复的关系,而且我不会绕过它。 我把我的代码留在下面,我对 mongoose 和 nosql db 有一些经验,但这对我来说是新的。 谢谢!

charModel.js

const { Model, DataTypes } = require('sequelize');

const CHARACTER_TABLE = 'character';

const CharacterSchema = {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    allowNull: false,
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  age: {
    type: DataTypes.BIGINT,
    allowNull: false,
  },
  weight: {
    type: DataTypes.BIGINT,
    allowNull: false,
  },
  history: {
    type: DataTypes.STRING,
    allowNull: false
  },
  image: {
    type: DataTypes.STRING,
    allowNull: false
  },
};

模型电影.js

class Character extends Model {
    static associate(models){
      this.belongsToMany(models.Movie, {
        as: "movies",
        through: "MovieCharacter",
        foreignKey: "characterId",
        otherKey: "movieId",
      });
    }
    static config(sequelize){
        return {
            sequelize,
            tableName: CHARACTER_TABLE,
            modelName: 'Character',
            timestamps: false,
        }
    }
}

const moment = require("moment");
const { Model, DataTypes } = require("sequelize");
const { GENRES_TABLE } = require("./genre.model");

const MOVIES_TABLE = "movie";

const MovieSchema = {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    allowNull: false,
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
  },
  creationDate: {
    field: "creation_date",
    type: DataTypes.STRING,
    get(){
      return moment(this.getDataValue('creationDate')).format('DD-MM-YYYY')
    },
    allowNull: false,
  },
  rating: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
  image: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  type: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  genreId: {
    field: "genre_id",
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: GENRES_TABLE,
      key: "id",
    },
    onUpdate: "CASCADE",
    onDelete: "SET NULL",
  },
};

class Movie extends Model {
  static associate(models) {
    this.belongsToMany(models.Character, {
      as: "characters",
      through: "MovieCharacter",
      foreignKey: "movieId",
      otherKey: "characterId",
    });
    this.belongsTo(models.Genre, {
      as: "genre",
    });
  }
  static config(sequelize) {
    return {
      sequelize,
      tableName: MOVIES_TABLE,
      modelName: "Movie",
      timestamps: false,
    };
  }
}

module.exports = { MOVIES_TABLE, MovieSchema, Movie };

模型CharMov.js

const { Model, DataTypes} = require('sequelize');
const { CHARACTER_TABLE} = require('./character.model');
const { MOVIES_TABLE} = require('./movies.model')

const MOVIES_CHARACTERS_TABLE = 'movies_characters';


const MoviesCharactersSchema = {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    allowNull: false,
  },
  movieId:{
    field: 'movie_id',
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: MOVIES_TABLE,
      key: 'id',
    },
    onUpdate: 'CASCADE',
    onDelete: 'SET NULL'
  },
  characterId:{
    field: 'character_id',
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: CHARACTER_TABLE,
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'SET NULL'
  }
};


class MovieCharacter extends Model {
  static config(sequelize) {
    return {
      sequelize,
      tableName: MOVIES_CHARACTERS_TABLE,
      modelName: 'MovieCharacter',
      timestamps: false
    };
  }
}

在联结表上添加 2 个 BelongsTo 关联,这些关联指向使用它来相互查找的表。 这将是我的第一个猜测,因为与您的代码相比,如果实现此代码,这是我的代码中唯一不同的地方。 如果这不起作用,请先生提供更多详细信息🧐

暂无
暂无

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

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