gpt4 book ai didi

mysql - 使用关联创建 Sequelize

转载 作者:行者123 更新时间:2023-11-29 16:24:31 25 4
gpt4 key购买 nike

我一直在尝试定义 3 个表之间的关系,然后在一个创建函数中创建它们。由于某种原因,在创建 3 个模型时,链接 ID(外键)未定义且未传递。以下是关联:

Person.js:

models.person.Lead = models.person.hasMany(models.lead, {
onDelete: "CASCADE",
foreignKey: "person_id"
});

Lead.js:

models.lead.Person = models.lead.belongsTo(models.person, {foreignKey: 'person_id'});

models.lead.Sealant_customer = models.lead.hasOne(models.sealant_customer, {
onDelete: "CASCADE",
foreignKey: 'lead_id'
})

sealantCustomer.js:

models.sealant_customer.Lead = models.sealant_customer.belongsTo(models.lead);

构建函数:

let sealantCustomer = models.sealant_customer.build({
address: body.address,
city: body.city,
roof_size: body.roofSize,
last_sealed: body.lastSealed,
existingSealant: body.existingSealant,
leaks_freq: body.leaksFrequency,
floor: body.floor,
elevator: body.elevator,
panels: body.panels,
home_type: body.homeType,
urgency: body.urgency,
next_step: body.nextStep,
more_info: body.moreInfo,
lead: {
site,
url: body.url,
date,
ip: body.ip,
person: {
name: body.name,
email: body.email,
phone: body.phone,
date,
city: body.city ? body.city : undefined,
address: body.address ? body.address : undefined,
}
}
}, {
include: [{
model: models.lead,
association: models.sealant_customer.Lead,
include: [{
model: models.person,
association: models.lead.Person
}]
}]
})

输出的对象很好,除了 Lead_id 和 person_id 为空(每个模型都有自己的 ID,但没有关联模型的 id)。我还应该注意到没有验证错误并且数据良好。

最佳答案

据我所知,该库的构建功能存在错误。与 create 相同的语法完美地工作。

关于mysql - 使用关联创建 Sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com