我在尝试在我的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
吗?你能明白为什么会这样吗?
似乎公司和团队之间有1:M的关系。您需要使用单数,因为一个团队属于单个公司,而不是多个公司。
t.belongs_to :company, :index => true, :foreign_key => true