我有两个表:书籍和文章,它们之间有多对多的关系。联接表是BookArticles。
models / books.js
module.exports = function(sequelize, DataTypes) { return Food = sequelize.define("Book", { id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false, autoIncrement: true, unique: true } }); }
models / articles.js
module.exports = function(sequelize, DataTypes) { return Food = sequelize.define("Article", { id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false, autoIncrement: true, unique: true } }); }
models / bookArticles.js
module.exports = function(sequelize, DataTypes) { return Food = sequelize.define("BookArticles", { id: { type: DataTypes.INTEGER, primaryKey: true, allowNull: false, autoIncrement: true, unique: true }, bookId: { type: DataTypes.INTEGER, references: 'Book', referencesKey: 'id', allowNull: false }, ArticleId: { type: DataTypes.INTEGER, references: 'Article', referencesKey: 'id', allowNull: false }, }); }
和models / index.js
m.BookArticles.belongsTo(m.Book); m.Book.hasMany(m.Article, {through: m.BookArticles}); m.BookArticles.belongsTo(m.Article); m.Article.hasMany(m.Books, {through: m.BookArticles});
但我无法获得书籍文章
我怎么才能得到它 ??
已针对Sequelize v2 / 3/4/5更新
通常,我认为问题在于我们对创建的表以及关联获得的方法感到困惑。
注意:定义foreignKey或交叉表名称是可选的。Sequelize自动创建它,但是对其进行定义,使编码人员可以读取模型并找出外键/交叉表名称是什么,而不用猜测或需要访问数据库。
// foreign key has to be defined on both sides. Parent.hasOne(Child, {foreignKey: 'Parent_parentId'}) // "Parent_parentId" column will exist in the "belongsTo" table. Child.belongsTo(Parent, {foreignKey: 'Parent_parentId'})
Parent.hasMany(Child, {foreignKey: 'Parent_parentId'}) Child.belongsTo(Parent, {foreignKey: 'Parent_parentId'})
Parent.belongsToMany( Child, { // this can be string (model name) or a Sequelize Model Object Class // through is compulsory since v2 through: 'Parent_Child', // GOTCHA // note that this is the Parent's Id, not Child. foreignKey: 'Parent_parentId' } ) /* The above reads: "Parents" belongs to many "Children", and is recorded in the "Parent_child" table, using "Parents"'s ID. */ Child.belongsToMany( Parent, { through: 'Parent_Child', // GOTCHA // note that this is the Child's Id, not Parent. foreignKey: 'Child_childId' } )
为什么要冗长的“ Parent_parentId”,而不仅仅是“ parentId”?这很明显表明它是属于“父母”的外键。在大多数情况下,可以使用更简洁的“ parentId”。*
DB.Parent.findOne({ where: { id: 1 }, include: [ DB.Child ] }).then(parent => { // you should get `parent.Child` as an array of children. })
关联提供了数据访问对象(DAO)方法:
hasOne():
在设置中Parent.hasOne(Child),可用于parentDAO实例的方法:
Parent.hasOne(Child)
parent
DB.Parent.findOne({ where: { id: 1 } }).then(parent => { // `parent` is the DAO // you can use any of the methods below: parent.getChild parent.setChild parent.addChild parent.createChild parent.removeChild parent.hasChild }) #### hasMany(): In setting a `Parent.hasMany(Child)`, methods available to `parent` DAO instance: ```js parent.getChildren, parent.setChildren, parent.addChild, parent.addChildren, parent.createChild, parent.removeChild, parent.hasChild, parent.hasChildren
归属于()/ belongsToMany:
在设置中Child.belongsTo(Parent),可用于childDAO实例的方法:
Child.belongsTo(Parent)
child
child.getParent, child.setParent, child.createParent //belongsToMany child.getParents, child.setParents, child.createParents
亲生父母/子女
// a parent can have many children Parent.belongsToMany(Child, { as: 'Natural', through: 'Parent_Child', foreignKey: 'Parent_parentId' }) // a child must at least have 2 parents (natural mother and father) Child.belongsToMany(Parent, { as: 'Natural', through: 'Parent_Child', foreignKey: 'Child_childId' })
养育父母/子女
Parent.belongsToMany(Child, { as: 'Foster', through: 'Parent_Child', foreignKey: 'Parent_parentId' }) Child.belongsToMany(Parent, { as: 'Foster', through: 'Parent_Child', foreignKey: 'Child_childId' });
上面将Parent_Child使用NaturalId和创建交叉表FosterId。
Parent_Child
NaturalId
FosterId