I'm trying to use Sequelize with express.js and MySQL, but i cant get a one-to-one relation to work.
The API always responses the address_id with the integer informed in the field, but not with the infos in the customers_addresses table. I try to force the eager loading by using { include: { all: true }} , but with no success.
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:
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:
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:
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. I think we should call the model name that have associations with the customer table
customers.findAll({include: [{model:customer_addresses}]})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.