如何实现多对多关联续约

如何实现多对多关联续约

本文介绍了如何实现多对多关联续约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两张表:书和文章之间有多对多的关系。
加入表是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:{
类型:DataTypes.INTEGER,
primaryKey:true,
allowNull:false,
autoIncrement:true,
unique:true
}
});
}

models / bookArticles.js

  module.exports = function(Sequelize,DataTypes){
return Food = sequelize.define(BookArticles,{
id:{
类型: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});

但我无法获取书籍文章



如何获取?

解决方案

删除BookArticles模型和更新关系:

  m.Book.hasMany(m.Article,{through:'book_articles'}); 
m.Article.hasMany(m.Books,{through:'book_articles'});


I have two tables: Books and Articles with a many-to-many relationship between them.Joining table is 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
    },
  });
}

And 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});

but I could not get book articles

How can I get it ??

解决方案

delete BookArticles model and update relation to:

m.Book.hasMany(m.Article, {through: 'book_articles'});
m.Article.hasMany(m.Books, {through: 'book_articles'});

这篇关于如何实现多对多关联续约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:41