Problem coding a weak entity in sequelize

徘徊边缘 提交于 2020-01-14 06:00:51

问题


I am creating a cinema application. I have modeled the database on mySql but I am having trouble migrating it to Sequelize. I have followed the documentation but I am getting a lot of different errors.

I have tried using associations and indexes (as it should be). This is the model I am trying to make.

OCCUPIED_SEATS is composed of only two foreign keys and both make a unique index.

OCCUPIED_SEATS:

const SEATS = require("./Seats");
const SCREENING = require("./Screening");

const OCCUPIED_SEATS = sequelize.define("OCCUPIED_SEATS", {
    //SEATS_ID
    //SCREENING_ID
  },
  {
    indexes: [
      {
        unique: true,
        fields: [SEAT_ID, SCREENING_ID]
      }
    ],
    underscored: true
  }
);

module.exports = OCCUPIED_SEATS;

SEATS:

const OCCUPIED_SEATS = require("./Occupied_Seats");

const SEATS = sequelize.define("SEATS", {
    SEATS_ID: {       
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true
    },
    ROW: {
        type: Sequelize.STRING,
        allowNull: false,
    },
    COLUMN: {
        type: Sequelize.INTEGER,
        allowNull: false
    },
  },
  {
    underscored: true
  }
);

SEATS.hasMany(OCCUPIED_SEATS, {foreignKey: 'SEAT_ID'})

module.exports = SEATS;

SCREENING:

const OCCUPIED_SEATS = require("./Occupied_Seats");

const SCREENING = sequelize.define("SCREENING", {
    SCREENING_ID: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true
    },
    SCREENING_START_TIME: {
        type: Sequelize.TIME,
        allowNull: false,
    },
    DATE: {
        type: Sequelize.DATE,
        allowNull: false
    }
  },
  {
    underscored: true,
    indexes: [
      {
        unique: true,
        fields: [ROOM_ID, SCREENING_START_TIME, DATE]
      }
    ]
  }
);

SCREENING.hasMany(OCCUPIED_SEATS, {foreignKey: 'SCREENING_ID'});

module.exports = SCREENING;

The error I am getting when I try this is:

[💻] Error: SEATS.hasMany called with something that's not a subclass of Sequelize.Model

How should I code the model?


回答1:


Looks like in the new version of Sequelize you have to define your models through Sequelize.Model type:

class Seats extends Sequelize.Model {}
Seats.init({
    id: {       
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true
    },
    row: {
      type: Sequelize.STRING,
      allowNull: false,
    },
   ...
});
module.exports = Seats;

And then somewhere else:

Seats.hasMany(OccupiedSeatc, {foreignKey: 'SEAT_ID'})

See model definition docs and accociation docs.



来源:https://stackoverflow.com/questions/56694727/problem-coding-a-weak-entity-in-sequelize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!