私はこの問題を数時間苦労していますが、この混乱するエラーを修正できないようです。私は、結合されたテーブルとクエリを作成しようとしていると、ネストされたオブジェクトで結果を表示します。sequelize:アソシエーションが間違ったforeignKeyカラム名を参照しています
ので、表には、このようなものです:blogs
テーブルがblogs
テーブルに外部キーaccounts_id
を通じてaccounts
テーブルに関連付けられている、これは簡単に参加クエリのようですが、私はそれが考えるようsequelizeは、右の外部キー列名を使用することはできません
アカウント:
module.exports = function(sequelize, DataTypes) {
return sequelize.define('accounts', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
username: {
type: DataTypes.STRING,
allowNull: false
},
............
}, {
tableName: 'accounts',
timestamps: false,
freezeTableName: true
});
モデルが定義されてい
account_id
として外部キーは、この方法と呼ばれていますブログ:
module.exports = function(sequelize, DataTypes){
var model = sequelize.define("blogs", {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
content: {
type: DataTypes.STRING,
allowNull: false
},
accounts_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
............
}, {
tableName: "blogs",
timestamps: false,
underscored: true
});
model.belongsTo(sequelize.models.accounts);
sequelize.models.accounts.hasMany(model, {targetKey: "accounts_id"});
return model;
};
とクエリは、このようなものです:
var criteria = {
include: [$db.models.accounts]
};
return model.findAll(criteria);
それはError: ER_BAD_FIELD_ERROR: Unknown column 'blogs.account_id' in 'field list'