The problem is that I cannot get the hasOne relation to work, which does not want to load an object of type state.
All queries are executed in existing tables.
Here is the customer table, the main thing is the cst_state_type field:
module.exports = function(sequelize, DataTypes) { return sequelize.define('customer', { customer: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, allowNull: true, validate: { isNumeric: true } }, first_name: { type: DataTypes.STRING(100), validate: { isAlphanumeric: true } }, last_name: DataTypes.STRING(100), identity_code: { type: DataTypes.STRING(20), allowNull: true, validate: { isNumeric: true } }, note: DataTypes.STRING(1000), birth_date: DataTypes.DATE, created_by: DataTypes.INTEGER, updated_by: DataTypes.INTEGER, cst_type: DataTypes.INTEGER, cst_state_type: { type: DataTypes.INTEGER, } }, { tableName: 'customer', updatedAt: 'updated', createdAt: 'created', timestamps: true }); };
Cst_state_type table:
module.exports = function(sequelize, DataTypes) { return sequelize.define('StateType', { cst_state_type: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, validate: { } }, name: DataTypes.STRING(100), }, { tableName: 'cst_state_type', timestamps: false }); };
How are relationships described:
global.db.Customer.hasOne(global.db.StateType, { foreignKey: 'cst_state_type', as: 'state_type' }); global.db.StateType.belongsTo(global.db.Customer, { foreignKey: 'cst_state_type' });
And creating an impatient download request:
db.Customer.findAll( { include: [ { model: db.Address, as: 'addresses' }, { model: db.StateType, as: 'state_type' } ] }) .success(function (customers) { res.json(200, customers); }) .fail(function (error) { res.json(500, { msg: error }); });