Sequelize hasMany工作正常,但逆关系不起作用 [英] Sequelize hasMany is working fine but the inverse relation is not working

查看:57
本文介绍了Sequelize hasMany工作正常,但逆关系不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Sequelize处理Node Js(express Js)中的mysql关系.

I am trying to work with mysql relations in Node Js(express Js) using Sequelize.

User.hasMany(Post); 正常工作,但是当我尝试在Post模型中将其逆转时,例如: Post.belongsTo(User);

User.hasMany(Post); work just fine, but when i try to inverse it in Post model like: Post.belongsTo(User);

遇到此错误:抛出新的Error($ {source.name}.$ {_.lowerFirst(Type.name)}调用,该错误不是Sequelize.Model的子类);

got this error: throw new Error(${source.name}.${_.lowerFirst(Type.name)} called with something that's not a subclass of Sequelize.Model);

错误:post.belongsTo调用的不是Sequelize.Model的子类

Error: post.belongsTo called with something that's not a subclass of Sequelize.Model

用户模型,例如:

const Sequelize = require('sequelize');
const db = require('../config/db');

const Post = require('./Post');

const User = db.define('user', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        notNull: true,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING,
        notNull: true
    },
    email: {
        type: Sequelize.STRING,
        notNull: true
    },
    password: {
        type: Sequelize.STRING,
        notNull: true
    }

});

User.hasMany(Post);

module.exports = User;

并发布模型,例如:

const Sequelize = require('sequelize');
const db = require('../config/db');

const User = require('./User');

const Post = db.define('post', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        notNull: true,
        primaryKey: true
    },

    title: {
        type: Sequelize.STRING,
        notNull: true
    },
    description: {
        type: Sequelize.TEXT,
        notNull: true
    },
    author: {
        type: Sequelize.STRING,
        notNull: true
    }
});

Post.belongsTo(User);

module.exports = Post;

我该如何解决此问题?谢谢大家...

推荐答案

您应该像这样将模型定义导出更正为函数,并在每个模型定义函数中定义 associate 函数,然后将其全部调用模型在诸如database.js的某些模块中注册:

You should correct your model definition exports as functions and define associate function in each model definition function like this and call it all after all models are registered in some module like database.js:

user.js

module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('user', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        notNull: true,
        primaryKey: true
    },
   ...

  User.associate = function (models) {
    User.hasMany(models.Post)
  }

post.js

module.exports = (sequelize, DataTypes) => {
  const Post = sequelize.define('post', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        notNull: true,
        primaryKey: true
    },
  ...
  Post.associate = function (models) {
    Post.belongsTo(models.User)
  }

这篇关于Sequelize hasMany工作正常,但逆关系不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆