1
votes

Getting error with documents table include:

Include unexpected. Element has to be either a Model, an Association or an object.:

const users = await db.users.findAll({
    where: {
      profile_photo: { [Op.ne]: null },
    },
    include: [
      { 
        model: db.documents,
      }
    ],
});

Relevant parts of documents define and association:

const document = sequelize.define(
    'document',
    {
      id: {
        allowNull: false,
        primaryKey: true,
        type: DataTypes.UUID,
        defaultValue: sequelize.literal('uuid_generate_v4()'),
      },
      user_id: {
        type: DataTypes.UUID,
        references: {
          model: 'users',
          key: 'id',
        },
      },
    }
  );
  document.associate = function(models) {
    models.document.belongsTo(models.users, {
      foreignKey: {
        name: 'user_id',
        allowNull: false,
      },
    });
  };

PSequel returns foreign key on documents table as:

Key name: documents_user_id_fkey

Columns: user_id

Foreign table: users

Foreign columns: id

And this psql query returns documents for user:

SELECT D
FROM users U
INNER JOIN documents D ON D.user_id = U.id

This query includes query DOES work though with emails table, and returns emails for user:

const users = await db.users.findAll({
    where: {
      profile_photo: { [Op.ne]: null },
    },
    include: [
      { 
        model: db.emails,
      }
    ],
});

Relevant parts of emails define and association:

const Emails = sequelize.define(
    'emails',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: sequelize.literal('uuid_generate_v4()'),
        primaryKey: true,
      },
      user_id: {
        type: DataTypes.UUID,
        references: {
          model: 'users',
          key: 'id',
        },
        allowNull: false,
      },
  );

  Emails.associate = models => {
    models.emails.belongsTo(models.users, {
      onDelete: 'CASCADE',
      foreignKey: {
        name: 'user_id',
        allowNull: false,
      },
    });
  };

PSequel returns foreign key on emails table as:

Key name: emails_user_id_fkey

Columns: user_id

Foreign table: users

Foreign columns: id

Why isn't the documents table behaving the same as emails?

Note: I did change the Sequelize association for Documents from name: id to user_id recently, do I need to re-init that association somehow?

It was changed from:

    models.document.belongsTo(models.users, {
      foreignKey: {
        name: 'id',
        allowNull: false,
      },
    });

To:

    models.document.belongsTo(models.users, {
      foreignKey: {
        name: 'user_id',
        allowNull: false,
      },
    });
1

1 Answers

2
votes

You must reference to the right model name. You either change it to this:

const users = await db.users.findAll({
    where: {
      profile_photo: { [Op.ne]: null },
    },
    include: [
      { 
        model: db.document,
      }
    ],
});

Or you change it to this:

const document = sequelize.define('documents', ..

If you define as document you must include document and not documents.