Sequlize 关联问题, [英] Trouble with Sequlize associations,

查看:37
本文介绍了Sequlize 关联问题,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置与 sequelize 库的关联时遇到了一些麻烦.不断收到 SequelizeEagerLoadingError: Client is not associated to License!

Been having some trouble setting up associations with the sequelize library. Keep getting SequelizeEagerLoadingError: Client is not associated to License!

这是我的两个模型.

'use strict';

module.exports = (sequelize, DataTypes) => {
  const License = sequelize.define('License', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true,
      validate: { isUUID: 4 }
    },
    client_id: {
      type: DataTypes.UUID,
      validate: { isUUID: 4 }
    }
  }, {
    classMethods: {
      associate: function (models) {
        License.belongsTo(models.Client, { foriegnKey: 'client_id' });
      }
    }
  });
  return License;
};

'use strict';

module.exports = (sequelize, DataTypes) => {
  const Client = sequelize.define('Client', {
    id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.UUIDV4,
      primaryKey: true
    },
    name: { type: DataTypes.STRING }
  }, {
    classMethods: {
      associate: function (models) {
        Client.hasOne(models.License);
        Client.hasMany(models.Event);
        Client.hasMany(models.Clips);
      }
    }
  });
  return Client;
};

这是我设置的控制器,我知道模型工作正常,因为我使用 findbyID() 函数进行了测试.

And here is the controller I have setup, I know the model is working correctly because I tested with the findbyID() function which works.

'use strict';

const db = require('../models/index.js');

class Controller {
    constructor(router) {
        router.get('/:id', (req, res, next) => {

            // db.License.findById(req.params.id).then(function(license) {
            //     res.status(200).json(license);
            // }, function(err) {
            //     res.status(404).json({
            //         error: 'License does not exist!'
            //     });
            // });
            db.License.findAll({
                include: [{ model: db.Client }]
            })
            .then(function(license) {
                res.status(200).json(license);
            }, function(err) {
                console.log(err);
                res.status(404).json({
                    error: 'License does not exist!'
                });
            });
        });
    }
}

module.exports = router => new Controller(router);

帮助会很棒,感觉超级迷茫.sequelize.sync() 正在运行并且没有抛出任何错误.试图弄清楚我是否有命名问题,可能是因为某些东西需要大写或不大写,但事实并非如此.

Help would be great feeling super lost. sequelize.sync() is being run and isn't throwing any errors. Trying to figure out if I have a naming issue maybe because something needs to be capitalized or not capitalized and it isn't.

推荐答案

我在 Sequelize v4+ 中也遇到过这种错误.我通过一些小的改动修改了我的模型,希望对你有用.像这样编写许可证模型

I also face this kind of error in Sequelize v4+. I modify my models with some small change, Hope that will work for you. Write Licence model like this

'use strict';

module.exports = function (sequelize, DataTypes) {
    const License = sequelize.define('License', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
            validate: {isUUID: 4}
        },
        client_id: {
            type: DataTypes.UUID,
            validate: {isUUID: 4}
        }
    });
    License.associate = function (models) {
        License.belongsTo(models.Client, {foriegnKey: 'client_id'});
    };
    return License;
};

同样修改你的客户端,

'use strict';

module.exports = function (sequelize, DataTypes) {
    const Client = sequelize.define('Client', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        name: {type: DataTypes.STRING}
    });
    Client.associate = function (models) {
        Client.hasOne(models.License);
        Client.hasMany(models.Event);
        Client.hasMany(models.Clips);
    };

    return Client;
};

这篇关于Sequlize 关联问题,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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