Record Deletion for Polymorphic-Associated Models

Feature(s) impacted

Record Deletion (default route behavior)

Observed behavior

When deleting records using the default route behavior (RecordRemover), if the deleting record is part of a scoped, polymorphic association, the through table record is not deleted.

FA does handle record creation correctly for these scoped, polymorphic-associated models, e.g. creatring a new record automatically creates the correct through table record.

Expected behavior

Through table record should be deleted.

Context

OwnableLink Table:

owned_table: varchar(255)
owned_foreign_key: char(36)
owner_table: varchar(255)
owner_foreign_key: char(36)

(Simplified, example) Model relationships:

     Book.belongsToMany(Author, {
        through: {
          model: db.OwnableLink,
          unique: false,
          scope: {
            owned_table: Book.name,
            owner_table: Author.name
          }
        },
        foreignKey: 'owned_foreign_key',
        constraints: false
      })

      Book.addScope(Author.name, (ownerID) => {
        return {
          include: [{
            model: Author,
            attributes: [],
            where: {
              id: ownerID
            }
          }]
        }
      })

      Book.hasMany(db.OwnableLink, {
        foreignKey: 'owned_foreign_key',
        constraints: false,
        onDelete: 'cascade',
        hooks: true
      })

      Author.belongsToMany(Book, {
        foreignKey: 'owner_foreign_key',
        constraints: false,
        through: {
          model: db.OwnableLink,
          unique: false,
          scope: {
            owner_table: Author.name,
            owned_table: Book.name
          }
        },
        onDelete: 'cascade',
        hooks: true
      })
  • Agent : forest-express-sequelize v9.2.9
  • Database type: SQL

Hey @yeti182

I can see an old github issue on sequelize repository, which seems to be fixed in more recent versions.

Could you share your project name as well as the version of Sequelize you are using so I can check on my end?

Thanks, @jeffladiray - I’ll PM you our project name and details.

1 Like

After upgrading to v6 of Sequelize, the through models are created and deleted correctly! That was it!

Thanks so much for your help, @jeffladiray!

1 Like

Ok, thanks for letting us know :pray: