sequelize:迁移后无法设置对两个不同表的引用

4xy9mtcn  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(232)

我正在尝试设置从donorslist表到user表和requestblood表的引用。虽然references列(userid,requestbloodid)被添加到表中,但它无法在列中插入reference id'requestbloodid',并将其设置为null。
我正在使用
节点
mysql:2.16.0版本
续集:4.37.10 v
user.js模型文件:

const moment = require('moment');

module.exports = (sequelize, DataTypes) => {
const user = sequelize.define(
'user',
 {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false,
    defaultValue: DataTypes.UUIDV4,
  },
  fullname: {
    type: DataTypes.STRING(20),
    allowNull: false,       
  },
  gender: { type: DataTypes.CHAR(1), allowNull: false },
  latitude: {
    type: DataTypes.DECIMAL(20, 15),
    allowNull: false,
    validate: { min: -90, max: 90 },
  },
  longitude: {
    type: DataTypes.DECIMAL(20, 15),
    allowNull: false,
    validate: { min: -180, max: 180 },
  },
  email: {
    type: DataTypes.STRING(50),
    allowNull: true,
  },
  isglobalcontactshared: { type: DataTypes.BOOLEAN, defaultValue: false },
  createdAt: {
    type: DataTypes.DATE,
    get() {
      return moment.utc(new Date(), 'DD-MMM-YYYY h:mm a').format('DD-MMM-YYYY h:mm a');
    },
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: null,
   },
 },

 {
   freezeTableName: true,
 }
);

user.associate = function(models) {
user.belongsTo(models.contactdetails, {
  onDelete: 'CASCADE',
  hooks: true,
  foreignKey: {
    name: 'contactdetailid',
    allowNull: false,
    foreignKeyConstraint: true,
  },
 });
};
 return user;
};

requestblood.js文件:

const moment = require('moment');

module.exports = (sequelize, DataTypes) => {
const requestblood = sequelize.define(
'requestblood',
 {
   id: {
     type: DataTypes.UUID,
     primaryKey: true,
     allowNull: false,
     defaultValue: DataTypes.UUIDV4,
  },
  requestid: {
    type: DataTypes.STRING(10),
    allowNull: false,
  },
  latitude: {
    type: DataTypes.DECIMAL(20, 15),
    allowNull: false,
    validate: { min: -90, max: 90 },
  },
  longitude: {
    type: DataTypes.DECIMAL(20, 15),
    allowNull: false,
    validate: { min: -180, max: 180 },
  },
  locality: { type: DataTypes.STRING(20), allowNull: false },
  city: { type: DataTypes.STRING, allowNull: false },
  bloodtype: {
    type: DataTypes.INTEGER,
    allowNull: false,
    isNumeric: true,
    validate: { min: 1, max: 9 },
  },
  duedate: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    get() {
      return moment.utc(this.getDataValue('duedate')).format('DD-MMM-YYYY');
    },
  },
  unitsrequired: { type: DataTypes.INTEGER, isNumeric: true },
  description: { type: DataTypes.STRING(500) },
  requeststateid: { type: DataTypes.STRING, allowNull: false },
  createdAt: {
    type: DataTypes.DATE,
    get() {
      return moment.utc(new Date(), 'DD-MMM-YYYY h:mm a').format('DD-MMM-YYYY h:mm a');
    },
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: null,
  },
},
{
  freezeTableName: true,
}
);

requestblood.associate = function(models) {
requestblood.belongsTo(models.contactdetails, {
  onDelete: 'CASCADE',
  hooks: true,
  foreignKey: {
    name: 'contactdetailid',
    allowNull: false,
    foreignKeyConstraint: true,
  },
 });
};

/* requestblood.associate = function(models) {
requestblood.hasMany(models.donorslist, {
  onDelete: 'CASCADE',
  hooks: true,
  foreignKey: {
    name: 'requestbloodid',
    allowNull: false,
    foreignKeyConstraint: true,
  },
});
}; */

return requestblood;
};

donorslist.js文件

const moment = require('moment');

module.exports = (sequelize, DataTypes) => {
const donorslist = sequelize.define(
 'donorslist',
 {
  id: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false,
    defaultValue: DataTypes.UUIDV4,
  },
  fullname: {
    type: DataTypes.STRING(20),
    allowNull: false,
  },
  bloodgroup: { type: DataTypes.INTEGER, allowNull: false },
  contactnumber: { type: DataTypes.STRING(13), allowNull: true },
  willdonate: { type: DataTypes.BOOLEAN, defaultValue: false },
  hasdonated: { type: DataTypes.BOOLEAN, defaultValue: false },
  createdAt: {
    type: DataTypes.DATE,
    get() {
      return moment.utc(new Date(), 'DD-MMM-YYYY h:mm a').format('DD-MMM-YYYY h:mm a');
    },
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: null,
  },
},
{
  freezeTableName: true,
 }
);

donorslist.associate = function(models) {
 donorslist.belongsTo(models.requestblood, {
  onDelete: 'CASCADE',
  hooks: true,
  foreignKey: {
    name: 'requestbloodid',
    allowNull: false,
    foreignKeyConstraint: true,
  },
});
};

donorslist.associate = function(models) {
donorslist.belongsTo(models.user, {
  onDelete: 'CASCADE',
  hooks: true,
  foreignKey: {
    name: 'userid',
    allowNull: false,
    foreignKeyConstraint: true,
   },
  });
};

return donorslist;
};

当执行以下代码时,

models.donorslist
.create({
  fullname,
  bloodgroup: bloodgrp,
  contactnumber: contnum,
  willdonate: req.body.willdonate,
  userid: usrid,
  requestbloodid: reqid,
  updatedAt: null,
 })

`它将查询返回为:

INSERT INTO `donorslist` (`id`,`fullname`,`bloodgroup`,`contactnumber`,`willdonate`,`hasdonated`,`createdAt`,`updatedAt`,`userid`) VALUES ('687888be-4381-460f-af62-46365a16fb40','sunil',4,'+123456789',true,false,'2019-01-10 07:00:12','2019-01-10 07:00:12','b93cfd73-a6ea-4825-91ad-8ded37418ca7');

请注意缺少requestbloodid列。它以某种方式跳过了requestbloodid列。在执行迁移之后,我面临这个问题。这在sequelize.sync()上运行良好。如果我在requestblood.js文件中取消对以下代码的注解,那么requestblood表的referenceid'contactdetailid'设置为null。因此,我无法从这两张表中找到belongsto和hasmany的参考资料。

requestblood.associate = function(models) {
requestblood.hasMany(models.donorslist, {
  onDelete: 'CASCADE',
  hooks: true,
  foreignKey: {
    name: 'requestbloodid',
    allowNull: false,
    foreignKeyConstraint: true,
  },
});
};

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题