Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.2k views
in Technique[技术] by (71.8m points)

mysql - New to Sequelize, how should I go about writing a seed file given a self referencing belongsToMany relationship?

table schemas. I have 3 mysql tables using sequelize, Person, Location, and Relationship. Location is very simple, with location_id being a foreign key to Person. Person belongs to many Person through a Relationship, ie a Mother person would have a dependent relationship to a student person. I am struggling to write a seed file that would establish these connections however:

Person model:

'use strict';

const RELATIONSHIP_TYPES = require('../utils/relationshipTypes');

let typeValues = Object.values(RELATIONSHIP_TYPES);

module.exports = (sequelize, DataTypes) => {
  const person = sequelize.define('person', {
    name: { type: DataTypes.STRING, allowNull: false },
    type: { type: DataTypes.ENUM({ values: typeValues }), allowNull: false, defaultValue: typeValues[0] },
    dateOfBirth: { type: DataTypes.CHAR, field: 'date_of_birth' },
    email: { type: DataTypes.STRING },
    phone: { type: DataTypes.STRING },
    intakeqId: { type: DataTypes.INTEGER, allowNull: false, field: 'intakeq_id' },
    kareoId: { type: DataTypes.INTEGER, field: 'kareo_id' }
  });

  person.associate = (models) => {
    // Many to one relationship with location
    models.location.hasMany(person, {
      foreignKey: 'location_id'
    });
    person.belongsTo(models.location);

    // Many-to-many relationship with other people
    person.belongsToMany(person, {
      as: { singular: 'dependent', plural: 'dependents' },
      through: models.relationship,
      foreignKey: { name: 'person_id', allowNull: false }
    });
    person.belongsToMany(person, {
      as: { singular: 'dependency', plural: 'dependencies' },
      through: models.relationship,
      foreignKey: { name: 'dependent_person_id', allowNull: false }
    });
  };

  return person;
};

Relationship model:

'use strict';

const RELATIONSHIP_TYPES = require('../utils/relationshipTypes');

let typeValues = Object.values(RELATIONSHIP_TYPES);

module.exports = (sequelize, DataTypes) => {
  const relationship = sequelize.define('relationship', {
    id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: false },
    type: { type: DataTypes.ENUM({ values: typeValues }), allowNull: false, defaultValue: typeValues[0]}
  });

  return relationship;
};

table people;

CREATE TABLE `people` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `type` enum('PATIENT','MOTHER','FATHER','TEACHER') NOT NULL DEFAULT 'PATIENT',
  `date_of_birth` char(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `phone` varchar(255) DEFAULT NULL,
  `intakeq_id` int NOT NULL,
  `kareo_id` int DEFAULT NULL,
  `location_id` int NOT NULL,
  PRIMARY KEY (`id`),
  KEY `location_id` (`location_id`),
  CONSTRAINT `people_ibfk_1` FOREIGN KEY (`location_id`) REFERENCES `locations` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

table relationships;

CREATE TABLE `relationships` (
  `id` int NOT NULL AUTO_INCREMENT,
  `type` enum('PATIENT','MOTHER','FATHER','TEACHER') NOT NULL DEFAULT 'PATIENT',
  `person_id` int NOT NULL,
  `dependent_person_id` int NOT NULL,
  PRIMARY KEY (`id`),
  KEY `person_id` (`person_id`),
  KEY `dependent_person_id` (`dependent_person_id`),
  CONSTRAINT `relationships_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `people` (`id`),
  CONSTRAINT `relationships_ibfk_2` FOREIGN KEY (`dependent_person_id`) REFERENCES `people` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

table locations;

CREATE TABLE `locations` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `hours` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...