sequelize.js - 无法在 Feathers-Sequelize 中使用一对一关联填充 PostgreSQL 表

标签 sequelize.js feathers-sequelize

我有两张 table :用户账户 .一个用户有一个帐户,反之亦然。这是我的两个模型:

用户型号

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;
};

从这些模型中,我得到一个 用户表和 账户 具有名为 user_id 的附加列的表.

我有一系列创建前的钩子(Hook):hashPassword , hashSecurityQuestions , 和 relateAccount .前两个工作完美无缺。第三个是我遇到问题的地方。这是我的relateAccount钩:

创建 Hook 前的关联帐户
module.exports = function (options = {}) {
  return function (context) {
    const AccountModel = context.app.services.accounts.Model;
    context.params.sequelize = {
      include: [{ model: AccountModel }]
    };
  };
};

通过上述设置,我可以将新用户插入 用户表使用 Postman 但 账户 表仍然是空的。这是一个示例 postman 请求:
{
    "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.hasOne(models.accounts);来自 用户型号我收到一条错误消息 error: SequelizeEagerLoadingError: accounts is not associated to users!
显然我做错了什么,我想我不理解我在 Sequelize 文档和 Feathers 文档中读到的内容。

最佳答案

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

在我的请求中,我需要制作 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
        }

}

关于sequelize.js - 无法在 Feathers-Sequelize 中使用一对一关联填充 PostgreSQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946210/

相关文章:

javascript - 未处理的拒绝 SequelizeDatabaseError : column "foo_bar" of relation "bar" already exists

node.js - Sequelize 连接表错误 : DatabaseError [SequelizeDatabaseError]: column does not exist

node.js - 带有 Feathers.js 和 Sequelize 的只读模式

sequelize.js - 羽毛 Sequelize 不适用于 `` `原始 : false ``` anymore after upgrading to Buzzard

node.js - 如何处理feathersjs钩子(Hook)内的 promise ?

mysql - Sequelize : Error: Error: Table1 is not associated to Table2

javascript - Sequelize : How to get record with createdat greater than 1 hour

node.js - sequelize 错误模型与另一个模型无关

reactjs - 羽毛 : Customizing find() for two different queries

postgresql - Feathers-Sequelize 与 Postgres : How to query an Array