Search code examples
sequelize.jsfeathers-sequelize

Unable to Populate PostgreSQL Table Using a One to One Association in Feathers-Sequelize


I have two tables: Users and Accounts. One user has one account and vice versa. Here are my two models:

User Model

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

Account Model

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

From these models I get a Users table and an Accounts table that has an additional column called user_id.

I have a series of before create hooks: hashPassword, hashSecurityQuestions, and relateAccount. The first two work flawlessly. The third one is where I'm having problems. Here is my relateAccount hook:

relateAccount before create hook

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

With the above setup I am able to insert a new user into the Users table using Postman but the Accounts table remains empty. Here is a sample Postman request:

{
    "username": "doctordonna",
    "firstname": "Donna",
    "lastname": "Noble",
    "email": "[email protected]",
    "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
        }

}

Like I said above it works perfectly when I change my associations to a One to Many however, I want this to be a One to One. If I remove user.hasOne(models.accounts); from the User Model I get an error saying error: SequelizeEagerLoadingError: accounts is not associated to users!

Obviously I'm doing something wrong and I guess I'm not understanding what I've read in both the Sequelize documentation and the Feathers documentation.


Solution

  • Ugh. This one was a really stupid mistake on my part.

    In my request I needed to make accounts singular- account. So my request should look like:

    {
        "username": "doctordonna",
        "firstname": "Donna",
        "lastname": "Noble",
        "email": "[email protected]",
        "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
            }
    
    }