x不与sequ​​elize 4.0中的y关联

时间:2018-06-28 06:11:57

标签: mysql node.js sequelize.js models

模型用户如下:

module.exports = (sequelize, Sequelize) => {

  var user = sequelize.define('user', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      });

       user.associate = function (models) {
          user.hasOne(models.oAuthAuthorizationToken, {foreignKey: 'userId', sourceKey: 'id'}); 
        };
});

模型oAuthAuthorizationToken看起来像

module.exports = (sequelize, Sequelize) => {
  var oauthAuthorizationToken = sequelize.define('oauthAuthorizationToken', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: Sequelize.INTEGER
    },
    userId: {
      type: Sequelize.UUID,
      references: {
        model: 'users',
        key: 'id'
      },
      allownull: false,
    }
  });
   oauthAuthorizationToken.associate = function(models) {
     oauthAuthorizationToken.belongsTo(models.user, { foreignKey: 'userId', targetKey: 'id' });
  };
  return oauthAuthorizationToken;
};

错误为 SequelizeEagerLoadingError:用户未与oauthAuthorizationToken关联! 已经有一个问题,但这不能解决我的问题。谢谢

0 个答案:

没有答案