Sequelize 字段关联不起作用
Sequelize field association does not work
我有两个 table。如何在 Sequelize 中第一个 table 中的 link 字段和第二个 table 中的 id
字段?
第一个table:
tables.comments = sequelize.define('comments', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
text: Sequelize.TEXT,
article: Sequelize.INTEGER,
author: {
type: Sequelize.INTEGER,
references: "users",
referencesKey: "id",
allowNull: false
},
answer: Sequelize.INTEGER,
rating: {
type: Sequelize.INTEGER,
defaultValue: 0
}
}, {
classMethods: {
associate: function(models) {
tables.comments.belongsTo(tables.models.users, {foreignKey: 'author', targetKey: 'name'});
}
}
});
第二个table:
tables.users = sequelize.define('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
mail: Sequelize.TEXT,
name: Sequelize.TEXT,
pass: Sequelize.TEXT,
status: {
type: Sequelize.INTEGER,
defaultValue: 0
},
rating: {
type: Sequelize.INTEGER,
defaultValue: 0
},
ban: {
type: Sequelize.INTEGER,
defaultValue: 0
},
key: Sequelize.TEXT
}, {
classMethods: {
associate: function(models) {
tables.users.hasMany(tables.models.comments, {foreignKey: 'author'});
}
}
});
我需要获取 tables.comments,但是 "author" 将是 tables.users 作者的名字。我提出要求:
tables.comments.findAll({inclide: [{model: tables.users}]}).then(function(comments) {
console.log(comments);
});
但是在字段 author
中的结果只有数字,而不是来自 users
的名字!
哪里错了?
(抱歉英语不好)
在关联类方法中,您需要使用定义类方法的 table 的列名。我假设连接条件是 comments.author = users.id
。
对于您的评论 table,它将是:
tables.comments.belongsTo(tables.models.users, {
foreignKey: 'author'
});
对于您的用户 table,它将是:
tables.users.hasMany(tables.models.comments, {
foreignKey: 'id'
});
我有两个 table。如何在 Sequelize 中第一个 table 中的 link 字段和第二个 table 中的 id
字段?
第一个table:
tables.comments = sequelize.define('comments', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
text: Sequelize.TEXT,
article: Sequelize.INTEGER,
author: {
type: Sequelize.INTEGER,
references: "users",
referencesKey: "id",
allowNull: false
},
answer: Sequelize.INTEGER,
rating: {
type: Sequelize.INTEGER,
defaultValue: 0
}
}, {
classMethods: {
associate: function(models) {
tables.comments.belongsTo(tables.models.users, {foreignKey: 'author', targetKey: 'name'});
}
}
});
第二个table:
tables.users = sequelize.define('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
mail: Sequelize.TEXT,
name: Sequelize.TEXT,
pass: Sequelize.TEXT,
status: {
type: Sequelize.INTEGER,
defaultValue: 0
},
rating: {
type: Sequelize.INTEGER,
defaultValue: 0
},
ban: {
type: Sequelize.INTEGER,
defaultValue: 0
},
key: Sequelize.TEXT
}, {
classMethods: {
associate: function(models) {
tables.users.hasMany(tables.models.comments, {foreignKey: 'author'});
}
}
});
我需要获取 tables.comments,但是 "author" 将是 tables.users 作者的名字。我提出要求:
tables.comments.findAll({inclide: [{model: tables.users}]}).then(function(comments) {
console.log(comments);
});
但是在字段 author
中的结果只有数字,而不是来自 users
的名字!
哪里错了?
(抱歉英语不好)
在关联类方法中,您需要使用定义类方法的 table 的列名。我假设连接条件是 comments.author = users.id
。
对于您的评论 table,它将是:
tables.comments.belongsTo(tables.models.users, {
foreignKey: 'author'
});
对于您的用户 table,它将是:
tables.users.hasMany(tables.models.comments, {
foreignKey: 'id'
});