समस्या यह है कि मैं संबंध में काम नहीं कर सकता हूं, जो राज्य प्रकार ऑब्जेक्ट को लोड करने में उत्सुक नहीं है।सीक्वेलिज़ एसोसिएशन के पास है, कोई है
सभी प्रश्न मौजूदा टेबल पर किए जाते हैं।
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 तालिका:
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
});
};
कैसे संबंधों वर्णित हैं:
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'
});
और उत्सुक बनाने
यहाँ ग्राहक की मेज, cst_state_type
क्षेत्र क्या महत्वपूर्ण है लोडिंग क्वेरी:
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 });
});
उत्कृष्ट उदाहरण! सरल और कार्यात्मक। –