Migracje Rails 3: dodanie kolumny referencyjnej?

Jeśli stworzę nową migrację rails 3 z (na przykład)

rails g migration tester title:tester user:references
Wszystko działa dobrze...jeśli jednak dodam kolumnę z czymś w stylu:
rails g migration add_user_to_tester user:references

Pole odniesienia nie jest rozpoznawane. Krótko mówiąc, pytanie brzmi: jak dodać kolumnę odniesienia do migracji rails z wiersza poleceń?

Author: Simone Carletti, 2011-02-10

10 answers

Jeśli używasz Rails 4.x możesz teraz wygenerować migracje z referencjami, jak to:

rails generate migration AddUserRefToProducts user:references

Jak widać na prowadnice rails

 203
Author: Paulo Fidalgo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-29 13:55:28

EDIT: jest to nieaktualna odpowiedź i nie powinna być stosowana dla Rails 4.x +

Nie musisz dodawać referencji, gdy możesz użyć integer id do odwołanej klasy.

Powiedziałbym, że zaletą używania referencji zamiast zwykłej liczby całkowitej jest to, że model będzie predefiniowany belongs_to, a ponieważ model jest już utworzony i nie będzie miał wpływu na migrację czegoś istniejącego, cel jest jakby stracony.

Więc chciałbym to zamiast:

rails g migration add_user_id_to_tester user_id:integer

A następnie ręcznie dodać belongs_to: user w modelu testera

 186
Author: DanneManne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-04 06:29:37

Pamiętaj, że najprawdopodobniej będziesz potrzebował indeksu również w tej kolumnie.

class AddUserReferenceToTester < ActiveRecord::Migration
  def change
    add_column :testers, :user_id, :integer
    add_index  :testers, :user_id
  end
end
 101
Author: Eugene,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-10 21:54:01

Z dwoma poprzednimi krokami podanymi powyżej, nadal brakuje ograniczenia klucza obcego. Powinno działać:

  class AddUserReferenceToTester < ActiveRecord::Migration
      def change
          add_column :testers, :user_id, :integer, references: :users
      end
  end
 50
Author: Martin Cabrera Diaubalick,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-30 16:04:38

Możesz używać referencji w migracji zmian. To jest poprawny kod Rails 3.2.13:

class AddUserToTester < ActiveRecord::Migration
  def change
    change_table :testers do |t|
      t.references :user, index: true 
    end
  end
  def down
    change_table :testers do |t|
      t.remove :user_id
    end
  end
end

C. f.: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table

 35
Author: gl03,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-06-26 12:45:37

Uruchomienie rails g migration AddUserRefToSponsors user:references wygeneruje następującą migrację:

def change
  add_reference :sponsors, :user, index: true
end
 27
Author: Wirwing,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-10 21:52:30

Podczas dodawania kolumny musisz uczynić z niej liczbę całkowitą i jeśli to możliwe trzymać się konwencji rails. Więc dla Twojego przypadku zakładam, że masz już Tester i modele użytkowników, i testerów i tabel użytkowników.

Aby dodać klucz obcy należy utworzyć kolumnę całkowitą o nazwie user_id (convention):

add_column :tester, :user_id, :integer

Następnie dodaj belongs_to do modelu testera:

class Tester < ActiveRecord::Base
  belongs_to :user
end

I możesz też chcieć dodać indeks dla klucza obcego (to jest coś, co odniesienia już robi dla Ciebie):

add_index :tester, :user_id
 8
Author: Zamith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-03 12:58:24

That will do the trick:

rails g migration add_user_to_tester user_id:integer:index
 8
Author: masterweily,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-07-03 10:06:24

Możesz dodawać referencje do swojego modelu za pomocą wiersza poleceń w następujący sposób:

rails g migration add_column_to_tester user_id:integer

Wygeneruje to plik migracji:

class AddColumnToTesters < ActiveRecord::Migration
  def change
    add_column :testers, :user_id, :integer
  end
end
To działa dobrze za każdym razem, gdy go używam..
 3
Author: Neha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-29 08:54:19

Dla Rails 4

Generator przyjmuje typ kolumny jako referencje(dostępne również jako belongs_to).

Ta migracja utworzy user_id kolumnę i odpowiedni indeks:

$ rails g migration AddUserRefToProducts user:references 

Generuje:

class AddUserRefToProducts < ActiveRecord::Migration
  def change
    add_reference :products, :user, index: true
  end
end

Http://guides.rubyonrails.org/active_record_migrations.html#creating-a-standalone-migration

Dla Rails 3

Helper nazywa się referencjami (dostępne również jako belongs_to).

Ta migracja będzie Utwórz kolumnę category_id odpowiedniego typu. Zauważ, że przekazujesz nazwę modelu, a nie nazwę kolumny. Active Record dodaje _id dla Ciebie.

change_table :products do |t|
  t.references :category
end

Jeśli masz polimorficzne skojarzenia belongs_to wtedy referencje dodadzą obie wymagane kolumny:

change_table :products do |t|
  t.references :attachment, :polymorphic => {:default => 'Photo'}
end

Doda kolumnę attachment_id i kolumnę string attachment_type z domyślną wartością Photo.

Http://guides.rubyonrails.org/v3.2.21/migrations.html#creating-a-standalone-migration

 3
Author: shilovk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-13 10:58:12