繁体   English   中英

设置Sequelize关联时出现问题-使用“ include”的查询失败

[英]Problem setting up Sequelize association - query with 'include' is failing

我是Sequelize的新手,它试图测试我在两个模型(用户模型和播客模型)之间建立的n:m关联是否正常工作。 当我尝试运行此查询时,出现某种数据库错误,该错误不是特定于什么错误的:

User.findOne({
    where: { id: id },
    include: [{ model: Podcast }]
});

有人知道我在搞砸吗? 我怀疑我建立关联的方式有问题,例如我略微错误地引用了表名,但是创建关联的迁移却可行。

这是我的User.js模型文件:

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    photo: {
      type: DataTypes.STRING
    }
  });
  User.associate = function(models) {
    // associations can be defined here
    User.belongsToMany(models.Podcast, {
      through: 'user_podcast'
    });
  };
  return User;
};

这是我的Podcast.js文件:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Podcast = sequelize.define('Podcast', {
    id: {
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    thumbnail: {
      type: DataTypes.STRING
    },
    website: {
      type: DataTypes.STRING
    }
  });
  Podcast.associate = function(models) {
    // associations can be defined here
    Podcast.belongsToMany(models.User, {
      through: 'user_podcast'
    });
  };
  return Podcast;
};

这是我为连接两个表而进行的迁移:

'use strict';
module.exports = {
  up: function(queryInterface, Sequelize) {
    return queryInterface.createTable('user_podcast', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      userId: {
        type: Sequelize.INTEGER,
        references: {
          model: 'Users',
          key: 'id'
        }
      },
      podcastId: {
        type: Sequelize.STRING,
        references: {
          model: 'Podcasts',
          key: 'id'
        }
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: function(queryInterface, Sequelize) {
    return queryInterface.dropTable('user_podcast');
  }
};

这是Github上的项目以供进一步参考: https : //github.com/olliebeannn/chatterpod

您无需为M:N表创建迁移。 现在,您的user_podcast模型出现了问题。 如果要在表之间设置M:N关系,则主键将是这两个模型的外键之间的组合。 如果您仍然希望表使用一个id主键,那么您将不在指向新模型user_podcast userpodcast模型上使用belongsToMany而是使用hasMany

据我在第一个查询中看到的,您似乎确实需要M:N关系,因此可以像对userpodcast一样定义模型,如下所示:

module.exports = (sequelize, DataTypes) => {
  const UserPodcast = sequelize.define('user_podcast', {
    userId: {
      // field: 'user_id', #Use 'field' attribute is you have to match a different format name on the db
      type: DataTypes.INTEGER
    },
    podcastId: {
      // field: 'podcast_id',
      type: DataTypes.INTEGER
    },
  });
  UserPodcast.associate = function(models) {
    models.User.belongsToMany(models.Podcast, { 
      as: 'podcasts',  //this is very important
      through: { model: UserPodcast }, 
      // foreignKey: 'user_id' 
    });
    models.Podcast.belongsToMany(models.User, { 
      as: 'users',      
      through: { model: UserPodcast }, 
      // foreignKey: 'podcast_id' 
    });
  };
  return UserPodcast;
};

我确实更喜欢在定义连接模型的保存函数上具有belongsToMany关联,并且您必须注意,我在该关联上as:属性。 这是非常重要的,因为这将有助于顺序地知道您在查询中指的是哪个关联。

User.findOne({
  where: { id: id },
  include: [{ 
    model: Podcast, 
    as: 'podcasts' //here I use the previous alias
  }]
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM