Ruby on rails 重写Rails 4和SQLite中的主键列

Ruby on rails 重写Rails 4和SQLite中的主键列,ruby-on-rails,ruby-on-rails-4,sqlite,rails-migrations,Ruby On Rails,Ruby On Rails 4,Sqlite,Rails Migrations,在谷歌搜索之后,我仍然无法找到一种成功的方法来覆盖Rails在使用SQLite时添加的主键。到目前为止,我有以下基本迁移: class CreateRequests < ActiveRecord::Migration def change create_table :requests, id: false do |t| t.string :id t.string :name end en

在谷歌搜索之后,我仍然无法找到一种成功的方法来覆盖Rails在使用SQLite时添加的主键。到目前为止,我有以下基本迁移:

class CreateRequests < ActiveRecord::Migration
    def change
        create_table :requests, id: false do |t|
            t.string :id
            t.string :name
        end    
    end
end
运行
rake db:migrate
后,我得到以下输出:

== 20140722082104 CreateRequests: migrating ===================================
-- create_table(:requests, {:id=>false})
   -> 0.0010s
-- execute("          DROP TABLE requests;\n          CREATE TABLE requests (id
     TEXT NOT NULL PRIMARY KEY, name TEXT);\n")
   -> 0.0010s
== 20140722082104 CreateRequests: migrated (0.0030s) ==========================
所以,看起来一切正常,但当我用SQLite管理程序检查数据库文件时,表
请求
不存在


我做错了什么?谢谢。

好的,我为自己找到了解决方案:独立执行每个SQL语句:

execute "DROP TABLE requests;"
execute "CREATE TABLE requests (id TEXT NOT NULL PRIMARY KEY, name TEXT);"
编辑

这是一个更优雅的解决方案,可以防止手动创建表,特别是当表有许多列,并且我们希望使其与ActiveRecord的
create\u table
方法的调用保持同步时:

#Get from SQLite's master table the SQL statement that creates the table, 
#and that was initially generated by Rails
sql = select_value("SELECT sql FROM sqlite_master WHERE type='table' AND name='requests'")

#Only replace the definition of the 'id' column by adding the PRIMARY KEY
#constraint
sql.gsub!(/"id" [^,]+/, '"id" VARCHAR(255) NOT NULL PRIMARY KEY')     

#Delete the original table            
drop_table :requests 

#Create the table again
execute sql
#Get from SQLite's master table the SQL statement that creates the table, 
#and that was initially generated by Rails
sql = select_value("SELECT sql FROM sqlite_master WHERE type='table' AND name='requests'")

#Only replace the definition of the 'id' column by adding the PRIMARY KEY
#constraint
sql.gsub!(/"id" [^,]+/, '"id" VARCHAR(255) NOT NULL PRIMARY KEY')     

#Delete the original table            
drop_table :requests 

#Create the table again
execute sql