Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Removal or Rewrite of disable_referential_integrity in AR

#2693

I suggest to remove disable_referential_integrity(&block) from all the adapters in ActiveRecord or redesign the specific behavior.

Even though I just submitted a patch to correct the conditions which can leave the database in inconsistent/erroneous states, I do not think that this method should exist in the way it is right now.
This opinion is shared by people in #postgresql on freenode.

Even though I suggest removal or redesign of all entities of this method (not just of the PostgreSQL-adapter) the reason stays the same.

It makes no sense to disable RI for any reason, at least not in the way it is handled. If the block is executed, the referential constraints are not checked against the new data (updated or inserted). Thus the best way for this would be to completely drop the constraints, yield the block and create them again.

If bad data (incorrect references) have been inserted, creating the constraint would fail, which would ensure database consistency.

I can write a new method which drops the constraints (if necessary other constraints too as well as indexes or other things), yields the block and then creates them again.

Also I'm curious as to what purpose this method serves? Right now I assume that the sole purpose is to insert large bulks quickly without the constraint being checked on each row.

Something like:

disable_referential_integrity {
populate_tables_with_lots_of_data() }

However, the best approach for exactly this would be as stated to remove the constraints (and especially indexes), insert the data, and recreate those constraints + indexes. I'm not sure about MySQL but this would be the only way to do things in PostgreSQL.
(A re-index is not a good option, usually a create index is faster if large bulks of data have been inserted).

So what do you think?

Reported by Victor Nawothnig · May 22nd, 2009 @ 12:18 AM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:51 PM

  3. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:51 PM

  4. bingbing