supose這個模式:squelize協會:查詢從衆多的最後createdAt協會一對多關聯
var user = sequelize.define('user', {
username: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING
}, {
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
});
var status = sequelize.define('status', {
status: Sequelize.STRING,
description: Sequelize.TEXT
}, {
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
});
var users_statuses = sequelize.define('users_statuses', {
user_id: Sequelize.INTEGER(11),
status_id: Sequelize.INTEGER(11),
}, {
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at'
});
user.hasMany(status, { foreignKey: 'user_id', through: models.users_statuses });
status.hasMany(user, { foreignKey: 'status_id', through: models.users_statuses });
所以每個用戶都有許多狀態(跟蹤所有選擇狀態的)。 使用查找與包含或getter user.getStatuses()我可以得到一個實際用戶的所有狀態,但我不知道如何獲得最後/當前關聯的狀態(基於created_at列users_statuses表/模型)
任何想法?
是啊,這解決了我的問題,thx! – MaK 2014-11-11 09:08:54