Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple belongsTo associations in same model to same target not working with inverse #15625

Closed
3 of 6 tasks
apstanisic opened this issue Jan 29, 2023 · 1 comment · Fixed by #15756
Closed
3 of 6 tasks
Assignees

Comments

@apstanisic
Copy link

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

When using multiple belongsTo associations in same model to same target with inverse, it complains that associations are not compatible.
Associations are defined from side where FK is located, with inverse property specified.
First associations is normally defined. But second is causing problem. Second associations works if inverse is removed.
I think that the problem is when defining second relation with inverse, it is not creating inverse relation properly, since it's comparing it with already existing 1st relation

Reproducible Example

Here is the link to the SSCCE for this issue: link
PR: link

 class Post extends Model {}
  class Author extends Model {}

  Post.init(
    {
      id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
      content: DataTypes.TEXT,
      authorId: DataTypes.INTEGER,
      coAuthorId: DataTypes.INTEGER,
    },
    {
      sequelize,
      modelName: "Post",
    }
  );

  Author.init(
    {
      id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
      name: DataTypes.TEXT,
    },
    {
      sequelize,
      modelName: "Author",
    }
  );

  Post.belongsTo(Author, {
    foreignKey: "authorId",
    as: "author",
    targetKey: "id",
    inverse: { as: "myBooks", type: "hasMany" },
  });

  Post.belongsTo(Author, {
    foreignKey: "coAuthorId",
    as: "coAuthor",
    targetKey: "id",
  // Code works if this line is removed
    inverse: { as: "notMyBooks", type: "hasMany" },
  });

What do you expect to happen?

To define 2 associations with inverse associations.

What is actually happening?

It throws an error saying that they are incompatible because their options are not reconcilable.
Error:

    [cause]: AssociationError [SequelizeAssociationError]: The association "notMyBooks" needs to define the BelongsTo association "Author" from Post to Author,
    but that child association has already been defined as BelongsTo, to Author by this call:
    
    Post.belongsTo(Author, {
      foreignKey: { name: 'authorId' },
      as: 'author',
      targetKey: 'id',
      inverse: { as: 'myBooks', type: 'hasMany' },
      hooks: false,
      name: { plural: 'authors', singular: 'author' }
    })
    
    That association would be re-used if compatible, but it is incompatible because their options are not reconcilable:
    
    Options of the association to create:
    {
      as: 'Author',
      foreignKey: { name: 'coAuthorId' },
      hooks: false,
      name: { plural: 'Authors', singular: 'Author' },
      targetKey: 'id'
    }
    
    Options of the existing association:
    {
      as: 'Author',
      foreignKey: { name: 'authorId' },
      hooks: false,
      name: { plural: 'Authors', singular: 'Author' },
      targetKey: 'id'
    }

Environment

  • Sequelize version: 7.0.0-alpha.20
  • Node.js version: v18.13.0
  • If TypeScript related: TypeScript version: 4.9.4, Not TS related
  • Database & Version: postgres 15 and sqlite from sscce
  • Connector library & Version: pg 8.9.0 in project, and default sqlite from sscce

Would you be willing to resolve this issue by submitting a Pull Request?

Open to take a look, but I think this is too deep in the library.

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

@apstanisic apstanisic added pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug labels Jan 29, 2023
@ephys
Copy link
Member

ephys commented Jan 30, 2023

Definitely sounds like a bug.
All associations are built on top of belongsTo (it's the association that actually handles the Foreign Key), but the inverse hasMany association should not be trying to re-create the belongsTo association - it already exists

As a temporary workaround, you should be able to define your association (+ inverse) using hasMany instead of belongsTo

PS: thank you for trying out Sequelize 7 😄

@ephys ephys removed the pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet label Jan 30, 2023
ephys added a commit that referenced this issue Mar 10, 2023
@ephys ephys self-assigned this Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
2 participants