Sequencing hasOne Associations, owned by To

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 }); }); 
+8
postgresql orm
source share
2 answers

I am sure that the error in your associations is somewhere. By how you described your table structure, associations should look something like this:

 global.db.Customer.belongsTo(global.db.StateType, { foreignKey: 'cst_state_type', as: 'state_type' }); global.db.StateType.hasMany(global.db.Customer, { foreignKey: 'cst_state_type' }); 

As I understand it, the same state can be assigned to many clients, so StateType.hasMany . The customer → statetype relationship is a "reverse association", which means that the foreign key is in the customer table. For this you need belongsTo

+2
source share

Thanks for the answer, it helped me a lot. You can also add relationships directly in your model using cool methods. I added an example below, hope this helps!

User Model (file)

 module.exports = function(sequelize, DataTypes){ var User = sequelize.define( 'User', { name: { type: DataTypes.STRING, allowNull: false } }, { classMethods:{ associate:function(models){ User.hasMany(models.Comment, { foreignKey: 'userId'} ); } } } ); return User; }; 

Comment Model (file):

 module.exports = function(sequelize, DataTypes){ var Comment = sequelize.define( 'Comment', { text: { type: DataTypes.STRING, allowNull: false } }, { classMethods:{ associate:function(models){ Comment.belongsTo(models.User, { foreignKey:'userId'} ); } } } ); return Comment; }; 

You do not need to set a foreign key, sequelize will process it if you do not specify it.

Then in the request:

 models.Comment.find({ where: { id: id }, include: [ models.User ], limit: 1 }) 
+18
source share

All Articles