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

Generated migration does not consider dependent: :destroy #24

Closed
DannyBen opened this issue Sep 18, 2015 · 1 comment
Closed

Generated migration does not consider dependent: :destroy #24

DannyBen opened this issue Sep 18, 2015 · 1 comment

Comments

@DannyBen
Copy link

Not sure if its an issue or a question more suitable for StackOverflow.

I have read the issues #18 and #20 so I figured maybe here is a good place to write.

  • I am using Rails 4.2.3
  • I have added the immigrant gem, without adding the foreigner gem.
  • My models have statements like has_many :invitations, dependent: :nullify and has_many :events, dependent: :destroy
  • After generating immigrant migrations, it generated all the add_foreign_key but without anything (on_delete or dependent ?... ) to reflect these dependencies.

So now, all my tests fail with update or delete on table "groups" violates foreign key constraint "steps_group_id_fk" on table "steps"

Have I missed a step? Or is this an issue?

@jenseng
Copy link
Owner

jenseng commented Sep 23, 2015

Immigrant does currently handle dependent: :delete, but I don't think it needs to handle dependent: :destroy, since you're basically saying "when I delete this thing, I want Rails to call destroy on each dependent model" (which could then trigger AR callbacks, etc.). So it may be unsafe/undesirable for a foreign key to cascade, if you somehow deleted the parent object via delete_all or whatever.

That said, Immigrant probably should handle dependent: :nullify. So we can keep this ticket open to address it.

For your particular testing issue though, it might have more to do with your code and how you're cleaning up data. Like maybe you've got a Group.delete_all that you could change to a Group.destroy_all? Or if you're just cleaning up all the things between tests, you might consider a different strategy. For example, DatabaseCleaner works well for this, and will do a single TRUNCATE statement with all the tables, so you don't have to worry about foreign keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants