Rails + UUID generated scheme assumes that the UUID is an integer, not a string

I am trying to use the UUID as the primary key for a Rails application, and I ran into a problem after the problem.

I specify the following in the migration: create_table: users ,: id => false do | t | then this is: execute ("ALTER TABLE ADD PRIMARY KEY (uuid) users")

In my user model: set_primary_key "uuid"

Using UUID tools to generate UUIDs.

This all works great, the problem I'm currently experiencing is that the generated schema.rb looks like this:

create_table "users" ,: primary_key => "uuid" ,: force => true do | t |

The primary key column is assumed to be an integer of 11 characters, not 36 characters, so the migrations performed create the correct database, but the test database is not generated correctly, and if I need to run rake db: schema: load, this will also fail ...

You need to figure out how to override the way that schema.rb assumes that if there is an integer in the primary key column ....

+6
ruby-on-rails
source share
4 answers

I think the best approach is to switch from managing your schema in Ruby (schema.rb) to managing it in SQL (development_structure.sql).

For this:

  • In application application.rb config.active_record.schema_format = :sql
  • Delete the schema.rb file.
  • Each time you run rake db:migrate , run rake db:dump:structure . This resets 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 migration guide (http://guides.rubyonrails.org/migrations.html).

+3
source share

We used the UUID as the primary key for a long time (currently with Rails 3.0.0), but still have not found a way to make the Scama dumper understand that there is no integer id.

Thus, our solution fixes the circuit manually when necessary. 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 

This is pretty annoying, but then everything works. Thus, the problem is not Rails, which does not allow the primary keys of the UUID, but the schemes cannot understand them.

+1
source share

I ran into this problem. As far as I can tell, it is impossible to override the requirement of the rails that PK is an integer. What I did to get around this was to add a key to the unique one in the database, and then set my default areas for each model to search through my unique string instead of the usual integer

0
source share

Found this, addresses the same problem of circuit dumps that don't understand UUIDs. Seems to work well.

http://code.google.com/p/uuid-schema-dumper/

-one
source share

All Articles