gpt4 book ai didi

mysql - "uninitialized constant"迁移过程中出错

转载 作者:行者123 更新时间:2023-11-29 12:07:21 28 4
gpt4 key购买 nike

我是 Ruby on Rails 的新手,我想使用迁移生成 mysql 数据库。

我尝试过这个命令

ruby bin/rake db:drop db:create db:migrate --trace

但我总是收到此错误:

    rake aborted!
StandardError: An error has occurred, all later migrations canceled:

uninitialized constant CreateOrganisation::Referential
C:/Sites/blog/db/migrate/20120823053740_create_organisation.rb:13:in `up'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:598:in `exec_migration'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with
_connection'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
C:in `migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transacti
on'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1039:in `ddl_transaction'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (req
uired)>'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
bin/rake:4:in `<main>'
Tasks: TOP => db:migrate

我尝试了以下命令:

rake db:rollback
rake db:reset
rake db:schema:dump
rake db:schema:load

但徒劳。

导致错误的文件(我猜)是:

class CreateOrganisation < ActiveRecord::Migration
def up
create_table :organisations do |t|
t.string :name
t.timestamps
end
change_table :referentials do |n|
n.belongs_to :organisation
end
change_table :users do |u|
u.belongs_to :organisation
end
Referential.reset_column_information
User.reset_column_information

organisation = Organisation.find_or_create_by_name!("Chouette")
Referential.update_all :organisation_id => organisation.id
User.update_all :organisation_id => organisation.id
end

def down
drop_table :organisations
end
end

我尝试使用 def change..end 而不是 def up..end/def down..end 但我仍然遇到相同的错误

我看不出我在这里缺少什么。

最佳答案

您正在尝试更改表引用,但它不存在:

change_table :referentials do |n|
n.belongs_to :organisation
end

该错误涉及未初始化的常量“:referentials”,该常量引用该特定表。

考虑使用生成器来创建迁移,直到您了解它们的工作原理;您可以随时调整生成的迁移或稍后从头开始编写自己的迁移。

示例

rails generate migration CreateFoo name:string amount:integer owner:string:index

这将创建

class CreateFoo < ActiveRecord::Migration
def change
create_table :foos do |t|
t.string :name
t.integer :amount
t.string :owner
end
add_index :foos, :owner
end
end

引用

我建议您阅读Rails Migration Guide ,这应该可以帮助您了解如何使用迁移。

关于mysql - "uninitialized constant"迁移过程中出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31250462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com