我试图设置从施主列表到用户表和requestblood表的引用。尽管将引用列(用户ID,requestbloodid)添加到表中,但无法在列中插入引用ID'requestbloodid'并将其设置为NULL。
我正在使用
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');
请注意缺少的requestbloodd列。它以某种方式跳过了requestbloodd列。执行迁移后,我面临此问题。使用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,
},
});
};