首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >序列化紧急加载错误: social_logins未关联到用户

序列化紧急加载错误: social_logins未关联到用户
EN

Stack Overflow用户
提问于 2020-08-28 09:27:34
回答 1查看 459关注 0票数 0

用户模型定义如下。

代码语言:javascript
复制
const db = require ('../../config/db_config');

const users = db.sequelize.define('users', {
  id: {
    type: db.DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  first_name: {
    type: db.DataTypes.STRING(150),
  },
  last_name: {
    type: db.DataTypes.STRING(150),
  },
  email: {
    type: db.DataTypes.STRING(256),
    required: true,
    unique: true
  },
  password: {
    type: db.DataTypes.STRING,
  },
  student_id: {
    type: db.DataTypes.STRING
  },
  status: {
    type: db.DataTypes.BOOLEAN,
    required: true,
    defaultValue: 0
  },
  is_deleted: {
    type: db.DataTypes.BOOLEAN,
    required: true,
    defaultValue: 0
  },
  createdAt: db.DataTypes.DATE,
  updatedAt: db.DataTypes.DATE,
});

module.exports = users;

定义如下的social_logins模型

代码语言:javascript
复制
const db = require ('../../config/db_config');

const socialLogins = db.sequelize.define('social_logins', {
  id: {
     type: db.DataTypes.INTEGER,
     primaryKey: true,
     autoIncrement: true,
  },
  token: {
    type: db.DataTypes.STRING
  },
  tokenType: {
    type: db.DataTypes.STRING
  },
  fb_id: {
    type: db.DataTypes.STRING
  },
  user_id: {
    type: db.DataTypes.INTEGER
  },
  is_deleted: {
    type: db.DataTypes.BOOLEAN,
    required: true,
    defaultValue: false
  }
}, { underscored: true, timestamp: true, tableName: 'social_logins' });

module.exports = socialLogins;

使用belongsTo函数与social_logins模型关联的用户模型

代码语言:javascript
复制
socialLoginsModel.belongsTo(users)

Sequelize抛出eagerLoadingError错误EagerLoadingError SequelizeEagerLoadingError: social_logins未关联到用户!

在运行下面给出的查询时。

代码语言:javascript
复制
const userModel = require ('./users_model');
const socialLoginModel = require('../social_logins/social_logins_model');

let id = "123456";
let email = "ex@example.com";

userModel.findOne({
  where: { email },
  include: [{
     model: socialLogins,
     where: {
       fb_id: id
     }
  }]
});
EN

回答 1

Stack Overflow用户

发布于 2020-08-30 04:01:24

根据您的模型名称,您应该具有如下所示的关联

基于数据库中定义的关系的socialLogins.belongsTo(用户)& users.hasOne(socialLogins)/ users.hasMany(socialLogins)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63626204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档