为什么此 Rails 迁移会出现 'no such table' 错误?
Why is this Rails migration giving a 'no such table' error?
在一个新的 Rails 6 项目中,我有一个名为 object_classes 的 table 和一个名为 ClassList_id 的列。来自 schema.rb:
create_table "object_classes", force: :cascade do |t|
t.string "name"
t.integer "ClassList_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ClassList_id"], name: "index_object_classes_on_ClassList_id"
end
我意识到应该将列命名为 class_list_id 以符合 Rails 预期的命名约定。因此,我生成了一个新的迁移:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
rename_column :object_classes, :ClassList_id, :class_list_id
end
end
但是,当我 运行 进行此迁移时,出现以下错误:
/bin/bash -c "env RBENV_VERSION=2.6.1 /home/asfarley/.rbenv/libexec/rbenv exec bundle exec ruby /home/asfarley/imgseq/bin/spring rails 'db:migrate'"
== 20200716060501 FixColumnName: migrating ====================================
-- rename_column(:object_classes, :ClassList_id, :class_list_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
我在这里做错了什么?我正在寻找专门解决此处问题的解释,以便我了解将来如何避免这种情况。
很难知道故障是 Rails 还是 SQLite,但问题似乎是我为我的 object_classes table 定义了一个指向 table 那不存在。通过这次迁移,我能够修复外键约束并重命名该列:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
remove_foreign_key :object_classes, :ClassLists
rename_column :object_classes, :ClassList_id, :class_list_id
add_foreign_key :object_classes, :class_lists
end
end
您的语法没问题,所以错误一定是保留名称错误。可能会尝试删除该列并再次创建该列,注意迁移中的大写字母,因为它们会使您的代码不必要地复杂化。试试这个:
rails g migration RemoveClassListIdFromObjectClasses
这将创建一个类似这样的迁移:
def drop
remove column :object_classes, :class_lis_id
end
然后是运行
rails g migration AddClassListIdToObjectClasses
迁移应如下所示:
def change
add_column :object_classes, :class_list_id
end
记得添加数据类型和您可能需要的任何其他内容。
在那里您可以检查它创建的迁移,您可以更改它以添加数据类型或更改列的大小写,然后您就可以开始了。
注意:如果有效,请采纳答案,谢谢!
在一个新的 Rails 6 项目中,我有一个名为 object_classes 的 table 和一个名为 ClassList_id 的列。来自 schema.rb:
create_table "object_classes", force: :cascade do |t|
t.string "name"
t.integer "ClassList_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ClassList_id"], name: "index_object_classes_on_ClassList_id"
end
我意识到应该将列命名为 class_list_id 以符合 Rails 预期的命名约定。因此,我生成了一个新的迁移:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
rename_column :object_classes, :ClassList_id, :class_list_id
end
end
但是,当我 运行 进行此迁移时,出现以下错误:
/bin/bash -c "env RBENV_VERSION=2.6.1 /home/asfarley/.rbenv/libexec/rbenv exec bundle exec ruby /home/asfarley/imgseq/bin/spring rails 'db:migrate'"
== 20200716060501 FixColumnName: migrating ====================================
-- rename_column(:object_classes, :ClassList_id, :class_list_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
我在这里做错了什么?我正在寻找专门解决此处问题的解释,以便我了解将来如何避免这种情况。
很难知道故障是 Rails 还是 SQLite,但问题似乎是我为我的 object_classes table 定义了一个指向 table 那不存在。通过这次迁移,我能够修复外键约束并重命名该列:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
remove_foreign_key :object_classes, :ClassLists
rename_column :object_classes, :ClassList_id, :class_list_id
add_foreign_key :object_classes, :class_lists
end
end
您的语法没问题,所以错误一定是保留名称错误。可能会尝试删除该列并再次创建该列,注意迁移中的大写字母,因为它们会使您的代码不必要地复杂化。试试这个:
rails g migration RemoveClassListIdFromObjectClasses
这将创建一个类似这样的迁移:
def drop
remove column :object_classes, :class_lis_id
end
然后是运行
rails g migration AddClassListIdToObjectClasses
迁移应如下所示:
def change
add_column :object_classes, :class_list_id
end
记得添加数据类型和您可能需要的任何其他内容。 在那里您可以检查它创建的迁移,您可以更改它以添加数据类型或更改列的大小写,然后您就可以开始了。
注意:如果有效,请采纳答案,谢谢!