【发布时间】:2018-08-21 01:33:03
【问题描述】:
我有一个最初使用 Django 创建的数据库,它是为单个外键关联设置的,here is the JSON dump.
现在这将被映射为:
export const Category = sequelize.define(
'category',
{
name: { type: Sequelize.STRING, unique: true },
},
{
timestamps: false,
tableName: 'ingredients_category',
}
);
export const Ingredient = sequelize.define(
'ingredient',
{
name: { type: Sequelize.STRING, unique: true },
notes: { type: Sequelize.TEXT, defaultValue: '' },
category_id: {
type: Sequelize.INTEGER,
references: {
model: Category,
key: 'id',
},
},
},
{
timestamps: false,
tableName: 'ingredients_ingredient',
}
);
Category.belongsToMany(Ingredient, {
foreignKey: 'category_id',
constraints: false,
through: 'ingredient_category',
});
Ingredient.hasOne(Category, {
// foreignKey: 'ingredientId',
constraints: false,
});
现在 sequelize 抱怨它在 Category 模型上缺少一个外键,但是虽然成分上只有一个类别,但类别并不限于该一个成分。有人可以帮助我理解这种关联吗?
更新:对于最终答案,这只是一个不正确的关联设置:
export const Category = sequelize.define(
'category',
{
name: { type: Sequelize.STRING, unique: true },
},
{
timestamps: false,
tableName: 'ingredients_category',
}
);
export const Ingredient = sequelize.define(
'ingredient',
{
name: { type: Sequelize.STRING, unique: true },
notes: { type: Sequelize.TEXT, defaultValue: '' },
},
{
timestamps: false,
tableName: 'ingredients_ingredient',
}
);
Category.hasMany(Ingredient, {
foreignKey: 'category_id',
});
Ingredient.belongsTo(Category, {
foreignKey: 'category_id',
});
【问题讨论】:
标签: javascript sqlite sequelize.js