gpt4 book ai didi

javascript - SequelizeJS HasOne 关联错误

转载 作者:行者123 更新时间:2023-12-03 22:41:27 25 4
gpt4 key购买 nike

我对 NodeJS 和 SequelizeJS 比较陌生,并且我正在构建的查询面临 hasOne 问题,我想知道您对此问题的看法,以找出我出错的地方以及实现此查询的正确方法。

Association Here

使用 sequelize-auto (pg-hstore) 生成的模型。

Bloco 模型:

module.exports = function(sequelize, DataTypes) {
return sequelize.define('bloco_condominio', {
id_bloco: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
id_condominio: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'condominio',
key: 'id_condominio'
}
},
nm_bloco: {
type: DataTypes.STRING,
allowNull: true
},
ic_status: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "A"
}
}, {
tableName: 'bloco_condominio'
});
};

公寓型号:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('apartamento', {
id_apartamento: {
type: DataTypes.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
id_condominio: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'condominio',
key: 'id_condominio'
}
},
nu_apto: {
type: DataTypes.STRING,
allowNull: true
},
id_bloco: {
type: DataTypes.INTEGER,
allowNull: true,
references: {
model: 'bloco_condominio',
key: 'id_bloco'
}
},
ic_status: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: "A"
},
dt_incl: {
type: DataTypes.TIME,
allowNull: false,
defaultValue: sequelize.fn('now')
},
dt_ult_alt: {
type: DataTypes.TIME,
allowNull: false,
defaultValue: sequelize.fn('now')
}
}, {
tableName: 'apartamento'
});
};

公寓服务:
"use strict";
var model = require('../models');
var Utils = require('../utils/utils');

var service = {};
var Apartamento = model.apartamento;
var Bloco = model.bloco_condominio;
var Morador = model.morador;
var Pessoa = model.pessoa;

//Incluir relação OneToMany
Apartamento.hasMany(Morador, { as: "Moradores", foreignKey: 'id_apartamento' });
Morador.belongsTo(Apartamento, { foreignKey: 'id_apartamento' });

Morador.hasMany(Pessoa, { as: "Pessoa", foreignKey: 'id_pessoa' });
Pessoa.belongsTo(Morador, { foreignKey: 'id_pessoa' });

Bloco.hasMany(Apartamento, { as: "Bloco", foreignKey: 'id_bloco' });
Apartamento.hasMany(Bloco, { foreignKey: 'id_bloco' });

service.getApartamentoById = function(idApartamento) {
return Apartamento.findById(idApartamento, {
include: [
{ model: Morador, as: 'Moradores', include: [
{ model: Pessoa, as: 'Pessoa'}
]},
{ model: Bloco, as: 'Bloco' }
]
})
.then(function(data) {
return data;
})
.catch(function(err) {
throw 'Erro ao consultar apartamento por ID: ' + err.message + ' - Request: '+JSON.stringify(idApartamento);
});
};

我可以完美地检索其他 hasMany 关联,但仍然没有找到相反的方法。

你们知道我应该如何以正确的方式解决这个问题吗?

在此先感谢您的帮助!

此致,
恩里科·贝加莫

最佳答案

为了让我更简单(只懂英语),我从谷歌翻译中获取了以下内容:

Pessoa: Person
Morador: Dweller
Bloco: Block
Apartmento: Apartment

所以,Dweller 可以有很多 People,一个 Apartment 可以有很多 Dwellers,一个 Block 可以有很多 Apartments。

你对其他模型的定义表明它们都是 1:m,所以我对公寓和街区遵循了这个假设。

考虑到这一点,以下应该有效。
Bloco.hasMany(Apartamento, { as: "Apartmento", foreignKey: 'id_bloco' });
Apartamento.belongsTo(Bloco, { foreignKey: 'id_bloco' });

Note: I've changed the as: "Bloco" to as: "Apartmento" and the second hasMany to belongsTo. This might be where your issues were coming from.



编辑: 访问属于一个街区的公寓的方法是:
bloco.getApartmento(options)

我有这个使用这个 promise 链:
Bloco.create()
.then(block => {
return Promise.all([
block,
Apartamento.bulkCreate([{
id_bloco: block.id_bloco
}, {
id_bloco: block.id_bloco
}, {
id_bloco: block.id_bloco
}, {
id_bloco: block.id_bloco
}, {}])
])
})
.spread((bloco, apartment) => {
return bloco.getApartamento()
})
.then(apartments => {
console.log(apartments.length); --> Logs 4 which matches the bulk create.
})

如果我误解了,它应该是 n:m 关系(公寓/街区),那么您应该在每个模型上使用belongsToMany 并确定 through 选项。
Bloco.belongsToMany(Apartamento, { 
as: "Apartmentos",
foreignKey: 'id_bloco',
through: "BlocoAparmento"
});
Apartamento.belongsToMany(Bloco, {
as: "Blocos",
foreignKey: 'id_apartmento',
through: "BlocoAparmento"
});

这将创建一个名为“BlockApartmento”的 n:m 连接表。如果您定义该模型,并使用模型而不是字符串,您将完全控制模型设置。

这将为您提供 Bloco.getApartmentos( 方法以及相反的( Apartmento.getBlocos( )以及 setAssociationaddAssoc...

http://docs.sequelizejs.com/manual/tutorial/associations.html#belongs-to-many-associations

关于javascript - SequelizeJS HasOne 关联错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44091727/

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