简体   繁体   中英

Cascade error with a mandatory one-to-many relationship

I am using Sails 1.1.0. I tried the below with sails-disk and sails-mongo adapter.

I created a mandatory-one-to-many rerlationship. A Sofa has owner which is one-to-many to User and a Sofa also has a home which is one-to-many with Home . However when I await Home.destroy(homeId); the Home , its not cascading down and deleting the Sofa . Instead I get error:

PropagationError: Failed to run the "cascade" polyfill.  Could not propagate the potential destruction of this home record.
Details:
  Cannot wipe the contents of association (`sofas`) because there is one conflicting shame record whose `home` cannot be set to `null`.  (That attribute is required.)

This error originated from the fact that the "cascade" polyfill was enabled for this query.
Tip: Try reordering your .destroy() calls.
 [?] See https://sailsjs.com/support for more help.

Here are my model associations:

// api\models\Sofa.js

    owner: {
      model: 'user',
      required: true
    },

    home: {
      model: 'home',
      required: true
    },

// api\models\User.js    

        sofas: {
           collection: 'sofa',
           via: 'owner'
        },

// api\models\Home.js    

        sofas: {
           collection: 'sofa',
           via: 'home'
        },

Does anyone have any ideas why this happens?

I got the same issue recently, using sails-disk adapter with inMemoryOnly: true option, you don't need to clean the database because is memory only and when test is finished it will be cleaned automatically.

datastores: {
  default: {
    adapter: 'sails-disk',
    inMemoryOnly: true
  },
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM