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.

Add foreign key support to migrations and schema.rb dump

#4347

This patch adds the following methods to migrations:

add_foreign_key(referencing_table, referenced_table, options = {})
remove_foreign_key(referencing_table, options)

change_table ... do |t|
  t.foreign_key(referenced_table, options)
  t.remove_foreign_key(options)
end

Options are :name, :column, :primary_key, :dependent.

In addition, ActiveRecord::SchemaDumper reads foreign keys from the SQL structure and puts them into schema.rb.


Some thoughts and concerns I came across while implementing this:

  • Extending t.references to automatically add a foreign key seems like a natural progression, but I didn't want to jam too much in at once.

  • The :primary_key value defaults to 'id'. This is a sensible default, but there is other code in the adapter that attempts to extract the primary key from the table. I do not know the history behind this, and was trying to clone the :primary_key API for associations.

  • Many active_record tests run with "use_transactional_fixtures = false", which results in leftover data during the foreign key tests. This makes adding foreign keys difficult, when the existing data has invalid references.

  • drop_table blows up if the table being dropped has foreign keys pointing to it. One option is to ignore foreign key checks during this (e.g. in MySql use "SET FOREIGN_KEY_CHECKS = 0; super; SET FOREIGN_KEY_CHECKS = 1;"

Reported by matthuhiggins · April 8th, 2010 @ 06:56 PM

State: new
Milestone: none
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. matthuhiggins
    matthuhiggins

    Oops, this patch left out tests for the schema dumper.

    April 8th, 2010 @ 07:27 PM

  2. matthuhiggins
    matthuhiggins

    foreign_keys-2.diff tests that foreign keys are dumped to schema.rb after the tables

    April 8th, 2010 @ 09:52 PM

  3. 2kan
    2kan
    1. I haven't read the patch, but it is very good idea.

    November 23rd, 2010 @ 11:48 AM

  4. wtn
    wtn

    -1

    I'm fine with using existing gems and leaving this out of the official AR.

    November 23rd, 2010 @ 11:58 AM

  5. Daniel
    Daniel

    +1 Migrations have always felt incomplete to me because they lack support for adding foreign key constraints. I greatly appreciate the Foreigner gem, but I would really like to see foreign keys supported in the framework.

    November 23rd, 2010 @ 08:16 PM

  6. Michał Łomnicki
    Michał Łomnicki

    +1 for foreign keys

    It is absolute must-have for ActiveRecord. Foreign keys are the only way to keep relations consistent as validations are prone to race conditions and one can't really rely on them.

    January 12th, 2011 @ 11:39 AM

  7. Ryan
    Ryan

    +1 This is absolutely necessary. Foreign keys should definitely be supported out of the box for migrations.

    January 14th, 2011 @ 02:24 AM

  8. Franco Catena
    Franco Catena

    +1 for me too. I agree, is the one thing that was always missed in migrations.

    February 15th, 2011 @ 10:20 PM

  9. bingbing