gpt4 book ai didi

database - sequelize js多级表级联连接

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:44 26 4
gpt4 key购买 nike

我将选择用户详细信息作为表结构。

Table user columns {user_id, region_id, user_name}

Table region columns {region_id, country_id }

Table country columns {country_id, country_name }

我加入表模型为

db.user.hasOne(db.region, {foreignKey:"region_id", targetKey:"region_id"});
db.region.hasOne(db.country,{ foreignKey :"country_id", targetKey:"country_id" });
db.user.hasOne(db.country,{through:db.region,otherKey :"country_id", foreignKey :"user_id" });

现在我要选择包含区域名称和国家名称的用户详细信息,这里是我的 Controller

 models.user.findAll(
{
include: [
{
model: models.region,
model: models.country
}
]
});

但是这个显示用户与国家没有关联

请救救我

最佳答案

它应该是多级连接作为你的模式

 models.user.findAll(
{
include: [
{
model: models.region,
include:[{model:models.country}]
}
]
});

前提是用户 belongsTo region , region belongsTo country你可以搜索更多关于关系的一些例子是

One-to-one relationship: You, as a User, can have one (hasOne) Profile. And of course the inverse also applies. Profile (belongsTo) a User. A user can't have more than one profile and a profile can't belong to multiple users.

还有

We can define the inverse of a hasOne relationship using the belongsTo method.

关于database - sequelize js多级表级联连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40561133/

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