[英]One-to-one relation on sequelize not working
I'm trying to use Sequelize with express.js and MySQL, but i cant get a one-to-one relation to work.我正在尝试将 Sequelize 与 express.js 和 MySQL 一起使用,但我无法获得一对一的工作关系。
The API always responses the address_id with the integer informed in the field, but not with the infos in the customers_addresses table. API 始终使用字段中通知的整数响应address_id ,但不使用customers_addresses表中的信息。 I try to force the eager loading by using { include: { all: true }} , but with no success.我尝试通过使用{ include: { all: true }}来强制加载,但没有成功。
Below is the files involved.以下是涉及的文件。
Can someone help ?有人可以帮忙吗? :) :)
How the two tables are created:如何创建两个表:
CREATE TABLE IF NOT EXISTS `customers_addresses`
(`address_id` INTEGER NOT NULL auto_increment , `numero_casa` VARCHAR(255),
`complemento` VARCHAR(255), `rua` VARCHAR(255), `bairro` VARCHAR(255), `cidade`
VARCHAR(255), `estado` VARCHAR(2), `cep` VARCHAR(9), PRIMARY KEY (`address_id`))
CREATE TABLE IF NOT EXISTS `customers` (`customer_id` INTEGER NOT NULL auto_increment ,
`first_name` VARCHAR(25) NOT NULL, `last_name` VARCHAR(50) NOT NULL, `cpf` CHAR(11) NOT NULL,
`rg` VARCHAR(20) NOT NULL, `expedidor_rg` VARCHAR(20) NOT NULL, `inss_number` INTEGER,
`inss_situation` INTEGER, `email` VARCHAR(30), `phone` VARCHAR(20), `gender` VARCHAR(1),
`civil_state` VARCHAR(255), `address_id` INTEGER, PRIMARY KEY (`customer_id`),
FOREIGN KEY (`address_id`) REFERENCES `customers_addresses` (`address_id`))
The init-models.js file: init-models.js 文件:
function initModels(sequelize) {
var customers = _customers(sequelize, DataTypes);
var customers_addresses = _customers_addresses(sequelize, DataTypes);
customers.hasOne(customers_addresses, { foreignKey: "address_id"});
customers_addresses.belongsTo(customers, { foreignKey: "address_id"});
return {
customers,
customers_addresses,
};
}
The customers.js model: customers.js 模型:
const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('customers', {
customer_id: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true
},
first_name: {
type: DataTypes.STRING(25),
allowNull: false
},
last_name: {
type: DataTypes.STRING(50),
allowNull: false
},
cpf: {
type: DataTypes.CHAR(11),
allowNull: false
},
rg: {
type: DataTypes.STRING(20),
allowNull: false
},
expedidor_rg: {
type: DataTypes.STRING(20),
allowNull: false
},
inss_number: {
type: DataTypes.INTEGER,
allowNull: true
},
inss_situation: {
type: DataTypes.INTEGER,
allowNull: true
},
email: {
type: DataTypes.STRING(30),
allowNull: true
},
phone: {
type: DataTypes.STRING(20),
allowNull: true
},
gender: {
type: DataTypes.STRING(1),
allowNull: true
},
civil_state: {
type: DataTypes.STRING(255),
allowNull: true
},
address_id: {
type: DataTypes.INTEGER,
notNull: true,
references: {
model: 'customers_addresses',
key: 'address_id'
}
}
}, {
sequelize,
tableName: 'customers',
timestamps: false
});
};
How the API controller calls it: API 控制器如何调用它:
customers.findAll({ include: { all: true }})
The query that is finally executed:最后执行的查询:
Executing (default): SELECT `customer_id`, `first_name`, `last_name`, `cpf`, `rg`, `expedidor_rg`, `inss_number`, `inss_situation`, `email`, `phone`, `gender`, `civil_state`, `address_id` FROM `customers` AS `customers`;
Try this in your API Controller.在您的 API 控制器中尝试此操作。 I think we should call the model name that have associations with the customer table我认为我们应该调用与客户表有关联的模型名称
customers.findAll({include: [{model:customer_addresses}]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.