I’m trying to use a UUID as the primary key for a Rails app, and am running into problem after problem.
I am specifying in migration this: create_table :users, :id => false do |t| then this: execute("ALTER TABLE users ADD PRIMARY KEY(uuid)")
In my user model: set_primary_key "uuid"
Using the UUID tools to generate the UUID.
This is all working great, the problem I currently have is that the schema.rb that gets generated looks like this:
create_table "users", :primary_key => "uuid", :force => true do |t|
Which assumes that the primary key column is an 11 character integer rather than a 36 character string, so running migrations produces a correct database, but test database is generated incorrectly, and if I were to run rake db:schema:load, it would fail as well...
Need to figure out how to override the way that schema.rb assumes that if there’s a primary key column that it will be an integer....
I think the best approach is to switch from managing your schema in Ruby (schema.rb) to managing it in SQL (development_structure.sql).
To do this:
config.active_record.schema_format = :sql
rake db:migrate
, run rake db:dump:structure
as well. That will dump your schema to db/development_structure.sql
. When you run rake db:test:prepare
it will now use the development_structure.sql
file instead of the scheam.rb
file.You can read more about this in section 6.2 of the migrations guide (http://guides.rubyonrails.org/migrations.html).
We have been using UUID as the primary key for a long time (currently with Rails 3.0.0), but so far have not found a way to make Schema dumper understand that there is no integer id.
So our solution has been fixing the schema by hand, when needed. For example always after rake db:migrate
. :D
We just change the line
create_table "people", :force => true do |t|
to
create_table "people", :id => false, :force => true do |t|
t.string "id", :limit => 22, :null => false
It's rather annoying but then everything works. So the problem is not in Rails not allowing UUID primary keys, but schema dumper not understanding those.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With