【发布时间】:2012-11-02 13:28:54
【问题描述】:
我是否正确地说 db/schema.rb 文件应该从 rake db:migrate 上的 db/migrate 文件中提取?我正在运行一个 rake db:migrate 并且它正在添加一个未在迁移中定义的表,也不是模型。有什么想法吗?
迁移文件(只有一个):
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :hashed_password
t.timestamps
end
end
end
rake 后的结果架构:
ActiveRecord::Schema.define(:version => 20121113214159) do
create_table "user_categories", :force => true do |t|
t.string "title"
t.string "description"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "users", :force => true do |t|
t.string "email"
t.string "hashed_password"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
end
我之前添加了一个 user_categories 脚手架,但不正确,因此我将其销毁。不知道我在破坏零件时哪里出错了......
【问题讨论】:
-
你能展示你的架构和你的迁移文件吗
-
就是这样——抱歉。另外,提到我之前实际上已经创建了脚手架,但我破坏了它。它不会读取数据库吗?我正在使用 SQLite,之前我收到了有关现有表的错误,但它不会让我删除它。
-
如果您的数据库中没有任何重要数据,您可以运行
rake db:drop然后运行rake db:create。然后运行rake db:migrate,它应该会更新您的架构。 -
成功了!不太清楚为什么 rake db:migrate 读取数据库...不应该只读取迁移文件吗?
-
rake db:migrate 不会按说读取数据库。如果您在架构文件上看到它有一个版本。该版本与最新迁移的时间戳匹配。不会读取早于您的架构版本的迁移来更新数据库。
标签: ruby-on-rails activerecord rake migrate