gpt4 book ai didi

sql - Sequelize 一对多关联错误

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

我目前正在开发一个新的 API,它将 SequelizeJS 与 PostgreDB 结合使用。我通常使用 MongoDB,但在让关联基于特定列(而非标准 ID)工作时遇到了一些麻烦。我想减少在常见操作中调用数据库的次数。

我有 3 个表,名称如下:Sites、Buildings、Areas。
- 站点有许多建筑物(站点:引用 <->建筑物:siteReference)
- 建筑有很多区域(建筑:引用 <-> 区域:建筑引用)
- 建筑属于 field
- 区域属于建筑物

我已经尽我所能地遵循了 Sequelize 文档(发现理解示例有点困难),并且相信以下内容会起作用(目前只是站点 <-> 建筑物):

Sites.hasMany(Buildings, { as: 'relatedBuildings', targetKey: 'siteReference'});
Buildings.belongsTo(Sites, { as: 'parentSite', sourceKey: 'reference'});

// Query function
return mainDB.Sites
.findAll({
raw: true,
include: [{
model: mainDB.Buildings,
as: 'relatedBuildings'
}]
})
.then(dbRes => { console.log(dbRes));
.catch(error => {console.log('ERROR', error.message)})

但是,当我运行上面的代码时,我收到以下错误:

First Error: relation "Sites" does not exist (note: the associations are stopping this table from being created)

Second Error:
"status": 409,
"message": "A database error occurred. Further details attached.",
"value": {
"name": "SequelizeDatabaseError",
"parent": {
"name": "error",
"length": 110,
"severity": "ERROR",
"code": "42P01",
"position": "1678",
"file": "parse_relation.c",
"line": "1160",
"routine": "parserOpenTable",
"sql": "SELECT \"Sites\".\"id\", \"Sites\".\"reference\", \"Sites\".\"name\", \"Sites\".\"description\", \"Sites\".\"addressLine1\", \"Sites\".\"addressLine2\", \"Sites\".\"city\", \"Sites\".\"county\", \"Sites\".\"postcode\", \"Sites\".\"country\", \"Sites\".\"lat\", \"Sites\".\"long\", \"Sites\".\"type\", \"Sites\".\"companyReference\", \"Sites\".\"namedContactReference\", \"Sites\".\"status\", \"Sites\".\"created_at\", \"Sites\".\"updated_at\", \"relatedBuildings\".\"id\" AS \"relatedBuildings.id\", \"relatedBuildings\".\"reference\" AS \"relatedBuildings.reference\", \"relatedBuildings\".\"name\" AS \"relatedBuildings.name\", \"relatedBuildings\".\"description\" AS \"relatedBuildings.description\", \"relatedBuildings\".\"addressLine1\" AS \"relatedBuildings.addressLine1\", \"relatedBuildings\".\"addressLine2\" AS \"relatedBuildings.addressLine2\", \"relatedBuildings\".\"city\" AS \"relatedBuildings.city\", \"relatedBuildings\".\"county\" AS \"relatedBuildings.county\", \"relatedBuildings\".\"postcode\" AS \"relatedBuildings.postcode\", \"relatedBuildings\".\"country\" AS \"relatedBuildings.country\", \"relatedBuildings\".\"lat\" AS \"relatedBuildings.lat\", \"relatedBuildings\".\"long\" AS \"relatedBuildings.long\", \"relatedBuildings\".\"type\" AS \"relatedBuildings.type\", \"relatedBuildings\".\"siteReference\" AS \"relatedBuildings.siteReference\", \"relatedBuildings\".\"namedContactReference\" AS \"relatedBuildings.namedContactReference\", \"relatedBuildings\".\"status\" AS \"relatedBuildings.status\", \"relatedBuildings\".\"created_at\" AS \"relatedBuildings.created_at\", \"relatedBuildings\".\"updated_at\" AS \"relatedBuildings.updated_at\", \"relatedBuildings\".\"site_id\" AS \"relatedBuildings.site_id\", \"relatedBuildings\".\"related_buildings_id\" AS \"relatedBuildings.related_buildings_id\" FROM \"Sites\" AS \"Sites\" LEFT OUTER JOIN \"Buildings\" AS \"relatedBuildings\" ON \"Sites\".\"id\" = \"relatedBuildings\".\"site_id\";"
},
"original": {
"name": "error",
"length": 110,
"severity": "ERROR",
"code": "42P01",
"position": "1678",
"file": "parse_relation.c",
"line": "1160",
"routine": "parserOpenTable",
"sql": "SELECT \"Sites\".\"id\", \"Sites\".\"reference\", \"Sites\".\"name\", \"Sites\".\"description\", \"Sites\".\"addressLine1\", \"Sites\".\"addressLine2\", \"Sites\".\"city\", \"Sites\".\"county\", \"Sites\".\"postcode\", \"Sites\".\"country\", \"Sites\".\"lat\", \"Sites\".\"long\", \"Sites\".\"type\", \"Sites\".\"companyReference\", \"Sites\".\"namedContactReference\", \"Sites\".\"status\", \"Sites\".\"created_at\", \"Sites\".\"updated_at\", \"relatedBuildings\".\"id\" AS \"relatedBuildings.id\", \"relatedBuildings\".\"reference\" AS \"relatedBuildings.reference\", \"relatedBuildings\".\"name\" AS \"relatedBuildings.name\", \"relatedBuildings\".\"description\" AS \"relatedBuildings.description\", \"relatedBuildings\".\"addressLine1\" AS \"relatedBuildings.addressLine1\", \"relatedBuildings\".\"addressLine2\" AS \"relatedBuildings.addressLine2\", \"relatedBuildings\".\"city\" AS \"relatedBuildings.city\", \"relatedBuildings\".\"county\" AS \"relatedBuildings.county\", \"relatedBuildings\".\"postcode\" AS \"relatedBuildings.postcode\", \"relatedBuildings\".\"country\" AS \"relatedBuildings.country\", \"relatedBuildings\".\"lat\" AS \"relatedBuildings.lat\", \"relatedBuildings\".\"long\" AS \"relatedBuildings.long\", \"relatedBuildings\".\"type\" AS \"relatedBuildings.type\", \"relatedBuildings\".\"siteReference\" AS \"relatedBuildings.siteReference\", \"relatedBuildings\".\"namedContactReference\" AS \"relatedBuildings.namedContactReference\", \"relatedBuildings\".\"status\" AS \"relatedBuildings.status\", \"relatedBuildings\".\"created_at\" AS \"relatedBuildings.created_at\", \"relatedBuildings\".\"updated_at\" AS \"relatedBuildings.updated_at\", \"relatedBuildings\".\"site_id\" AS \"relatedBuildings.site_id\", \"relatedBuildings\".\"related_buildings_id\" AS \"relatedBuildings.related_buildings_id\" FROM \"Sites\" AS \"Sites\" LEFT OUTER JOIN \"Buildings\" AS \"relatedBuildings\" ON \"Sites\".\"id\" = \"relatedBuildings\".\"site_id\";"
},
"sql": "SELECT \"Sites\".\"id\", \"Sites\".\"reference\", \"Sites\".\"name\", \"Sites\".\"description\", \"Sites\".\"addressLine1\", \"Sites\".\"addressLine2\", \"Sites\".\"city\", \"Sites\".\"county\", \"Sites\".\"postcode\", \"Sites\".\"country\", \"Sites\".\"lat\", \"Sites\".\"long\", \"Sites\".\"type\", \"Sites\".\"companyReference\", \"Sites\".\"namedContactReference\", \"Sites\".\"status\", \"Sites\".\"created_at\", \"Sites\".\"updated_at\", \"relatedBuildings\".\"id\" AS \"relatedBuildings.id\", \"relatedBuildings\".\"reference\" AS \"relatedBuildings.reference\", \"relatedBuildings\".\"name\" AS \"relatedBuildings.name\", \"relatedBuildings\".\"description\" AS \"relatedBuildings.description\", \"relatedBuildings\".\"addressLine1\" AS \"relatedBuildings.addressLine1\", \"relatedBuildings\".\"addressLine2\" AS \"relatedBuildings.addressLine2\", \"relatedBuildings\".\"city\" AS \"relatedBuildings.city\", \"relatedBuildings\".\"county\" AS \"relatedBuildings.county\", \"relatedBuildings\".\"postcode\" AS \"relatedBuildings.postcode\", \"relatedBuildings\".\"country\" AS \"relatedBuildings.country\", \"relatedBuildings\".\"lat\" AS \"relatedBuildings.lat\", \"relatedBuildings\".\"long\" AS \"relatedBuildings.long\", \"relatedBuildings\".\"type\" AS \"relatedBuildings.type\", \"relatedBuildings\".\"siteReference\" AS \"relatedBuildings.siteReference\", \"relatedBuildings\".\"namedContactReference\" AS \"relatedBuildings.namedContactReference\", \"relatedBuildings\".\"status\" AS \"relatedBuildings.status\", \"relatedBuildings\".\"created_at\" AS \"relatedBuildings.created_at\", \"relatedBuildings\".\"updated_at\" AS \"relatedBuildings.updated_at\", \"relatedBuildings\".\"site_id\" AS \"relatedBuildings.site_id\", \"relatedBuildings\".\"related_buildings_id\" AS \"relatedBuildings.related_buildings_id\" FROM \"Sites\" AS \"Sites\" LEFT OUTER JOIN \"Buildings\" AS \"relatedBuildings\" ON \"Sites\".\"id\" = \"relatedBuildings\".\"site_id\";"
}

最终,当我在 Sites 表上运行 FindAll() 时,我的目标是获得如下输出:

Site
- Building
- - Area
- - Area
- Building
- - Area
- Building
- - Area
- - Area
- - Area
Site
- Building
- - Area
- - Area
- Building
- - Area
- - Area
Site
- Building
- - Area
- - Area

我哪里错了?

更新 - 这是我的模型(注意我在别处正式定义它们)

网站

const Sites = {
name: 'Sites',
schema: {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
reference: {
type: DataTypes.STRING(100),
unique: true,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING(512),
allowNull: true
},
addressLine1: {
type: DataTypes.STRING,
allowNull: false
},
addressLine2: {
type: DataTypes.STRING,
allowNull: false
},
city: {
type: DataTypes.STRING,
allowNull: false
},
county: {
type: DataTypes.STRING,
allowNull: false
},
postcode: {
type: DataTypes.STRING,
allowNull: false
},
country: {
type: DataTypes.STRING,
allowNull: false
},
lat: {
type: DataTypes.STRING,
allowNull: false
},
long: {
type: DataTypes.STRING,
allowNull: false
},
type: {
type: DataTypes.STRING,
allowNull: false
},
companyReference: {
type: DataTypes.STRING(100),
allowNull: false
},
namedContactReference: {
type: DataTypes.STRING(100),
allowNull: true
},
status: {
type: DataTypes.STRING(20),
allowNull: false
}
},
options: {
underscored: true
}
};

export default Sites;

建筑物

const Buildings = {
name: 'Buildings',
schema: {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
reference: {
type: DataTypes.STRING(100),
unique: true,
allowNull: false
},
name: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING(256),
unique: false,
allowNull: true
},
addressLine1: {
type: DataTypes.STRING,
allowNull: false
},
addressLine2: {
type: DataTypes.STRING,
allowNull: false
},
city: {
type: DataTypes.STRING,
allowNull: false
},
county: {
type: DataTypes.STRING,
allowNull: false
},
postcode: {
type: DataTypes.STRING,
allowNull: false
},
country: {
type: DataTypes.STRING,
allowNull: false
},
lat: {
type: DataTypes.STRING,
allowNull: false
},
long: {
type: DataTypes.STRING,
allowNull: false
},
type: {
type: DataTypes.STRING,
allowNull: false
},
siteReference: {
type: DataTypes.STRING(100),
allowNull: false
},
namedContactReference: {
type: DataTypes.STRING(100),
allowNull: true
},
status: {
type: DataTypes.STRING(20),
allowNull: false
}
},
options: { underscored: true }
};
export default Buildings;

区域

const Areas = {
name: 'Areas',
schema: {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
reference: {
type: DataTypes.STRING(100),
unique: true,
allowNull: false
},
buildingReference: {
type: DataTypes.STRING(100),
unique: false,
allowNull: false
},
panelReference: {
type: DataTypes.STRING(100),
unique: false,
allowNull: false
},
name: {
type: DataTypes.STRING(128),
unique: false,
allowNull: false
},
description: {
type: DataTypes.STRING(512),
allowNull: true
},
drawingReference: {
type: DataTypes.STRING(100),
unique: false,
allowNull: false
},
status: {
type: DataTypes.STRING(10),
allowNull: false
}
},
options: { underscored: true }
};
export default Areas;

最佳答案

您是否在模型中设置了关联?

Please refer to the Sequelize docs at the section

例子

如果我对你的问题的理解正确,你想建立一个多对多的关系,换句话说就是 n:m。

使用 你可以通过在两个模型上设置一个关系来做到这一点

belongsToMany(Model,{through: 'tableNameYouWant'});


// In Your Site Model
Sites.belongsToMany(Buildings, {through: 'SiteBuildings'});

// In your Building Model
Buildings.belongsToMany(Sites, {through: 'SiteBuildings'});

// In your Area Model
Areas.belongsTo(Buildings)

如果这有助于解决您的问题,请告诉我!

关于sql - Sequelize 一对多关联错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48107278/

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