Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9775 (closed enhancement: fixed)

Opened 9 months ago

Last modified 9 months ago

[XPATCH] Add foreign_key as an type of column on migrations

Reported by: arthurgeek Assigned to: nzkoz
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: tiny
Cc:

Description

I used sexy_migrations plugin and the only thing I miss on Edge Rails is the foreign_key column type. This patch adds this option to migrations, so you can write code like this:

create_table :user do |t|

t.fkey :country ....

end

And a country_id column is created. This code is the same one on the sexy_migration.

Attachments

foreign_key_on_migrations.patch (0.8 kB) - added by arthurgeek on 10/03/07 21:11:53.
foreign_key_on_migrations_with_polymorphic_and_docs.patch (2.2 kB) - added by tom on 10/04/07 07:26:36.
references_on_migrations_with_polymorphic_and_docs.patch (2.2 kB) - added by arthurgeek on 10/17/07 04:41:39.
references_on_migrations_with_polymorphic_docs_and_tests.patch (3.3 kB) - added by arthurgeek on 10/19/07 01:09:44.
With tests
fix_typo_in_tests.patch (0.5 kB) - added by arthurgeek on 10/19/07 02:48:53.

Change History

10/03/07 21:11:53 changed by arthurgeek

  • attachment foreign_key_on_migrations.patch added.

10/04/07 07:26:00 changed by tom

  • summary changed from Add foreign_key as an type of column on migrations to [PATCH] Add foreign_key as an type of column on migrations.

If this were added at all it'd make sense for it to take a :polymorphic argument to also generate the _type column. This patch adds that functionality and some documentation.

10/04/07 07:26:36 changed by tom

  • attachment foreign_key_on_migrations_with_polymorphic_and_docs.patch added.

10/04/07 15:12:22 changed by arthurgeek

Nice addition!

10/04/07 17:47:59 changed by urubatan

+1

10/04/07 17:50:50 changed by urubatan

it would be great if the :ref option from sexy_migrations was supported too. I'll try to implement it today and send the patch but I need to improve my ruby skills

10/04/07 17:54:29 changed by arthurgeek

Urubatan, I don't know if the ref option from sexy_migrations is supported on all database adapters.

10/04/07 18:26:26 changed by tom

Plus that sort of thing is culturally verboten for Rails anyway; we don't believe in foreign keys round these parts. Maybe the method should be called #belongs_to?

10/05/07 17:05:45 changed by qertoip

+1, minor: personally I would remove fkey alias

10/05/07 19:21:37 changed by piclez

+1, sexier

10/05/07 19:22:59 changed by arthurgeek

  • keywords changed from tiny to tiny verified.

10/08/07 09:16:01 changed by chuyeow

+1 (on foreign_key_on_migrations_with_polymorphic_and_docs.patch). Lovely!

I'd also remove the fkey alias:

alias :fkey :foreign_key # Unnecessary

10/08/07 09:59:53 changed by norbert

Actually, I'd keep the alias. Nice feature.

10/15/07 05:02:43 changed by nzkoz

  • keywords changed from tiny verified to tiny.
  • owner changed from core to nzkoz.
  • status changed from new to assigned.
  • summary changed from [PATCH] Add foreign_key as an type of column on migrations to [XPATCH] Add foreign_key as an type of column on migrations.

Nice one guys, I don't like the name 'foreign_key' as that's not what we're creating.

How about:

t.references :post

with an alias for

t.belongs_to :taggable, :polymorphic=>true

Finally, needs tests! :) Address those and we're good to go

10/15/07 14:49:29 changed by arthurgeek

Someone can point me to tests for the other column types, like t.string, t.integer, and so on?

I haven't added a test for this patch since I haven't found tests for those ones..

10/15/07 21:03:22 changed by nzkoz

It would appear that we have no tests for that functionality.

MigrationTest contains some tests for t.column etc. So you could either expand on them, or perhaps whip up a simple mocha test?

  t.expects(:column).with(:customer_id, :integer)
  t.references :customer

10/16/07 03:42:11 changed by arthurgeek

I tried testing with mocha, using the following code, but get an failing test:

uses_mocha 'group_by_non_numeric_foreign_key_association' do
  def test_references_column_type_adds_id
    Person.connection.create_table :testings do |t|
      t.expects(:column).with(:customer_id, :integer)
      t.references :customer
    end
  end
end

I got this failure message:

 1) Failure:
test_references_column_type_adds_id(MigrationTest)
    [(eval):1:in `column'
     ./test/../lib/active_record/connection_adapters/abstract/schema_definitions.rb:445:in `references'
     ./test/../lib/active_record/connection_adapters/abstract/schema_definitions.rb:444:in `each'
     ./test/../lib/active_record/connection_adapters/abstract/schema_definitions.rb:444:in `references'
     ./test/migration_test.rb:876:in `test_references_column_type_adds_id'
     ./test/../lib/active_record/connection_adapters/abstract/schema_statements.rb:94:in `create_table'
     ./test/../lib/active_record/connection_adapters/mysql_adapter.rb:396:in `create_table'
     ./test/migration_test.rb:874:in `test_references_column_type_adds_id']:
#<Mock:0x36f29b4>.column('customer_id', :integer, {}) - expected calls: 0, actual calls: 1
Similar expectations:
column(:customer_id, :integer)

And I haven't found tests for the t.timestamps for example. I tried looking for this one and then modify mine, but...

10/16/07 23:16:50 changed by lawrence

-1 on fkey and foreign_key. +1 for suggestion to use references instead.

For more sexy migrations please also see #9881

10/17/07 01:52:29 changed by nzkoz

Upload your patch so far and I'll take a look at the mocha failure if you like

(follow-up: ↓ 20 ) 10/17/07 04:41:13 changed by arthurgeek

Fine, here goes.

It would be a good idea to improve the coverage and test the t.timestamps as well.

10/17/07 04:41:39 changed by arthurgeek

  • attachment references_on_migrations_with_polymorphic_and_docs.patch added.

10/18/07 06:33:23 changed by lawrence

+1 on last patch. A beauty!

(in reply to: ↑ 18 ) 10/18/07 07:08:05 changed by nzkoz

Replying to arthurgeek:

Fine, here goes. It would be a good idea to improve the coverage and test the t.timestamps as well.

I don't see te mocha test there?

10/19/07 01:09:15 changed by arthurgeek

Sorry, my bad!

10/19/07 01:09:44 changed by arthurgeek

  • attachment references_on_migrations_with_polymorphic_docs_and_tests.patch added.

With tests

10/19/07 02:09:09 changed by nzkoz

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [7973]) Add t.belongs_to and t.references to sexy migrations [arthurgeek] Test harness for Sexy Migrations. [Koz] Closes #9775

10/19/07 02:25:47 changed by nzkoz

Thanks for that, I've applied it now. The only difference was symbol vs strings and some empty hash args

10/19/07 02:48:12 changed by arthurgeek

I'm lazy today (and sleepy too).

There's a typo on my tests. Here goes another patch.

Sorry for that (again). :)

10/19/07 02:48:53 changed by arthurgeek

  • attachment fix_typo_in_tests.patch added.