0
我試圖在我的rails項目中運行遷移時出現問題。Rails數據庫遷移無法添加外鍵
我有一個簡單的加法兩個表:
class ModifyCurrentTablesToNewDesign < ActiveRecord::Migration
def change
# Some other migrations...
# New Table Companies
create_table :companies do |t|
t.string :name
end
# New Table Teams
create_table :teams do |t|
t.string :name
t.belongs_to :companies, :index => true, :foreign_key => true
end
# Some oooother migrations...
end
end
而當運行它,我有這樣的:
-- create_table(:companies)
-> 0.0036s
-- create_table(:teams)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedColumn: ERROR: column "company_id" referenced in foreign key constraint does not exist
: ALTER TABLE "teams" ADD CONSTRAINT "fk_rails_e080df8a94"
FOREIGN KEY ("company_id")
REFERENCES "companies" ("id")
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.2.3/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:762:in `add_foreign_key'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:217:in `block in create_table'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:216:in `each_pair'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/schema_statements.rb:216:in `create_table'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:661:in `block in method_missing'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `block in say_with_time'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:631:in `say_with_time'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:651:in `method_missing'
/Users/julian/Development/sikuani/plataforma-eventos/db/migrate/20160121164754_modify_current_tables_to_new_design.rb:35:in `change'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:605:in `exec_migration'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:589:in `block (2 levels) in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:588:in `block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:587:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:764:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `block in ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:1040:in `ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:955:in `block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `each'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:951:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:819:in `up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/migration.rb:797:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.2.3/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
是不應該的是在創建表時companies
,默認情況下在內部創建了一列id
?你能看到爲什麼會發生這種情況?
它的工作! ...但現在我很困惑:/,如果表被稱爲'公司'(複數),這種關係不在表名之間? – unmultimedio
這樣想一下,一個團隊屬於**單個公司**,而不是多個公司。 –