【问题标题】:Unable to Populate PostgreSQL Table Using a One to One Association in Feathers-Sequelize无法在 Feathers-Sequelize 中使用一对一关联填充 PostgreSQL 表
【发布时间】:2019-03-01 23:52:20
【问题描述】:

我有两个表:UsersAccounts。一个用户有一个帐户,反之亦然。这是我的两个模型:

用户模型

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const user = sequelizeClient.define('users', {

   // omitting the definition for brevity
  });


  user.associate = function (models) {

    user.hasOne(models.accounts);

    // I could get things to work when I made it a one to many
    // but that's not what I want.
    // user.hasMany(models.accounts, { foreignKey: 'user_id' });
  };

  return user;
};

帐户模型

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const account = sequelizeClient.define('accounts', {

    // omitting the definition for brevity

  });

  account.associate = function (models) {

    account.belongsTo(models.users, {foreignKey: 'user_id'});

    // I can't get groups and roles working either but that's
    // another question...
    account.belongsToMany(models.groups, {
      through: 'accountsingroups',
      foreignKey: 'accountid'
    });
    account.belongsToMany(models.roles, {
      through: 'accountsinroles',
      foreignKey: 'accountid'
    });
  };

  return account;
};

从这些模型中,我得到一个 Users 表和一个 Accounts 表,其中有一个名为 user_id 的附加列。

我有一系列创建前的钩子:hashPasswordhashSecurityQuestionsrelateAccount。前两个工作完美无缺。第三个是我遇到问题的地方。这是我的relateAccount 钩子:

创建钩子之前的relateAccount

module.exports = function (options = {}) {
  return function (context) {
    const AccountModel = context.app.services.accounts.Model;
    context.params.sequelize = {
      include: [{ model: AccountModel }]
    };
  };
};

通过上述设置,我可以使用 Postman 将新用户插入到 Users 表中,但 Accounts 表仍然为空。这是一个邮递员请求示例:

{
    "username": "doctordonna",
    "firstname": "Donna",
    "lastname": "Noble",
    "email": "besttempinchiswick@tardis.com",
    "password": "ood1!",
    "accounts":
        {
            "questiononeid": "1a3ccccb-42ff-4553-9e54-824022414f50",
            "questiononeanswer": "100 words per minute",
            "questiontwoid": "6f9b0a38-0b1f-46dd-8726-2c9ff3566686",
            "questiontwoanswer": "Adipose Industries",
            "questionthreeid": "95c7f66f-63ef-48b2-b2b4-d73d383077e8",
            "questionthreeanswer": "Vesuvius",
            "questionfourid": "ff94887b-6015-4b84-8259-4801b2b404a3",
            "questionfouranswer": "Time Vortex",
            "isapproved": true,
            "lastactivitydate": null,
            "lastlogindate": null,
            "lastpasswordchangeddate": null,
            "isonline": false,
            "islockedout": false,
            "lastlockedoutdate": null,
            "failedpasswordattemptcount": 0,
            "failedpasswordattemptwindowstart": null,
            "isdeactivated": false,
            "deactivated_at": null
        }

}

就像我上面所说的,当我将关联更改为 一对多 时,它可以完美运行,但是,我希望这是一个 一对一。如果我从 User Model 中删除 user.hasOne(models.accounts);,我会收到一条错误消息 error: SequelizeEagerLoadingError: accounts is not associated to users!

显然我做错了什么,我想我不理解我在 Sequelize 文档和 Feathers 文档中读到的内容。

【问题讨论】:

    标签: sequelize.js feathers-sequelize


    【解决方案1】:

    呃。这对我来说是一个非常愚蠢的错误。

    在我的请求中,我需要制作accounts 单数-account。所以我的请求应该是这样的:

    {
        "username": "doctordonna",
        "firstname": "Donna",
        "lastname": "Noble",
        "email": "besttempinchiswick@tardis.com",
        "password": "ood1!",
        "account":
            {
                "questiononeid": "1a3ccccb-42ff-4553-9e54-824022414f50",
                "questiononeanswer": "100 words per minute",
                "questiontwoid": "6f9b0a38-0b1f-46dd-8726-2c9ff3566686",
                "questiontwoanswer": "Adipose Industries",
                "questionthreeid": "95c7f66f-63ef-48b2-b2b4-d73d383077e8",
                "questionthreeanswer": "Vesuvius",
                "questionfourid": "ff94887b-6015-4b84-8259-4801b2b404a3",
                "questionfouranswer": "Time Vortex",
                "isapproved": true,
                "lastactivitydate": null,
                "lastlogindate": null,
                "lastpasswordchangeddate": null,
                "isonline": false,
                "islockedout": false,
                "lastlockedoutdate": null,
                "failedpasswordattemptcount": 0,
                "failedpasswordattemptwindowstart": null,
                "isdeactivated": false,
                "deactivated_at": null
            }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-09
      • 2019-05-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多