我正在使用节点js并使用claudia-api-builder作为框架在AWS中启动API来构建无服务器应用程序。
在app.js文件中,我将所需的api称为
driver.find_element_by_xpath("//*[contains(local-name(), 'button') and contains(@class, 'gprospect_linkedin_button')]").click()
下面是我的代码:
const ApiBuilder = require('claudia-api-builder');
const api = new ApiBuilder();
module.exports = api;
api.post('/api/auth/validatephonenumber', async function (request)
{
return new Promise((resolve, reject) => {
index.loadDatabase().then((db) => {
resolve(loginController.validatePhonenumber(db, request));
});
});
});
我试图将EmailRegistration表的emailregistrationid添加到ContactDetails表中作为外键引用。我正在将sequelize与mysql,nodejs配合使用以实现所需的结果。但是,我遇到了以下错误:
未处理的拒绝SequelizeForeignKeyConstraintError:无法添加或更新子行:外键约束失败(
async function validatePhonenumber(db, request) { return new Promise( async (resolve, reject) => { let emailid; await db.EmailRegistration.sync().then(function () { emailid = db.EmailRegistration.findOne({ where: { email: { [Op.eq]: mailid } }, attributes: ['id'], }); }); if (emailid != null) { console.log(`email id: ${emailid.id}`); await db.ContactDetails.sync().then(function () { db.ContactDetails.findOrCreate({ where: { contactnumber: phnum }, defaults: { EmailRegistrationId: emailid.id }, }).spread((contactdetails, created) => { console.log(`contactdetails: ${contactdetails}`); if (contactdetails !== null) { resolve({ statuscode: indexController.statusCode.statusOK, contactdetails: contactdetails }) } else { reject({ statuscode: indexController.statusCode.InternalServerError, message: 'phone number not created' }); } }); }); }; }); }
。inmeeydb
,CONSTRAINTContactDetails
外部键(ContactDetails_ibfk_1
)参考{ {1}}(EmailRegistrationId
)在删除CASCADE时在更新CASCADE上
以下是我的EmailRegistration模型文件:
EmailRegistration
下面是我的Contactdetails模型文件:
id
我试图在两个表中都按以下方式更改代码,但没有任何效果。
const moment = require('moment');
module.exports = (sequelize, DataTypes) => {
const EmailRegistration = sequelize.define(
'EmailRegistration',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
},
email: {
type: DataTypes.STRING(50),
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: { min: 6 },
},
createdAt: {
type: DataTypes.DATE,
get() {
return moment.utc(new Date(), 'DD/MM/YYYY h:mm a').format('DD/MM/YYYY h:mm a');
},
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: null,
},
},
{
freezeTableName: true,
}
);
EmailRegistration.associate = function (models) {
EmailRegistration.hasOne(models.ContactDetails,
{ foreignKey: 'EmailRegistrationId' }
);
};
return EmailRegistration;
};
无法分析如何解决此问题。当我将nodejs与expressjs一起使用且没有问题时,此方法工作正常。它无法在ContactDetails表中标识EmailRegistrationId(查询中缺少),并将输出显示为
const moment = require('moment');
module.exports = (sequelize, DataTypes) => {
const ContactDetails = sequelize.define(
'ContactDetails',
{
id: {
type: DataTypes.UUID,
primaryKey: true,
allowNull: false,
defaultValue: DataTypes.UUIDV4,
},
contactnumber: { type: DataTypes.STRING(13), allowNull: false },
isverified: { type: DataTypes.BOOLEAN, defaultValue: false },
createdAt: {
type: DataTypes.DATE,
get() {
return moment.utc(new Date(), 'DD/MM/YYYY h:mm a').format('DD/MM/YYYY h:mm a');
},
},
updatedAt: {
type: DataTypes.DATE,
defaultValue: null,
},
},
{
indexes: [{ fields: ['contactnumber'], unique: true }],
},
{
freezeTableName: true,
}
);
ContactDetails.associate = function(models) {
ContactDetails.belongsTo(models.EmailRegistration, {
onDelete: 'CASCADE',
hooks: true,
foreignKey: { allowNull: false },
});
};
return ContactDetails;
};
答案 0 :(得分:0)
您需要更新关联的配置。 ContactDetails
模型现在将具有一个名为emailregistrationid
EmailRegistration.associate = function (models) {
EmailRegistration.hasMany(models.ContactDetails);
};
ContactDetails.associate = function(models) {
ContactDetails.belongsTo(models.EmailRegistration, {
onDelete: 'CASCADE',
hooks: true,
foreignKey: {
name: 'emailregistrationid'
allowNull: false
},
});
}
ContactDetails.create({
...
emailregistrationid: <some_valid_emailregistrationid>
})