top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Migrate postgresql database to uuid in Ruby On Rails

+3 votes
187 views

I am using rails 3.1 and ruby 1.9.3,Now i want to use uuid concept in rails 3 for existing data

so I did like :-

create_table :posts, :id => false do |t|
 t.string :uuid, :limit => 36, :primary => true
end

ActiveRecord::Base.class_eval do

# old rails versions
set_primary_key 'uuid'

before_create :generate_uuid
def generate_uuid
self.id = UUIDTools::UUID.random_create.to_s
end
end

This is working for new data,now i want to migrate existing data with relation.for uuid they are using datatype as string,in postgresql the data type used for primary_key and foreign key is integer ,so if i am trying to change foreign key integer to string it is throwing error. Can someone please tell me some example,how to do this.

posted Oct 25, 2013 by Sumit Pokharna

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote

By default, a table on PostgreSQL does not register UUID...
It might work only for new tables you create, because to Postgres use UUID you need say it when creating the table... for existing ones, it might not happen because the table on PostgreSQL may not have been created with the option to use Uuids.

answer Oct 25, 2013 by Deepankar Dubey
Similar Questions
0 votes

Using PostgreSQL shell how to select the database and tables.

0 votes

Error Details:

Resolve "FATAL:no pg_hba.conf entry for host"

Anyone suggest how to solve this error?

0 votes

Hi,

I need to take the only schema from Postgresql. Can anyone suggest how to dumb or migrate the schema?

0 votes

I was reading the docs for primary key on postgresql and I saw that there was a way to add a custom stored procedure that returns a UUID. There is a test case as well that uses a custom uuid generator

https://github.com/rails/rails/blob/650ea5e5cf50d8a7242499463cf1762922d330a8/activerecord/test/cases/adapters/postgresql/uuid_test.rb#L193

But I was not sure how to to implement this myself. Like in the test case where do I place my_uuid_generator() and how to implement it in my rails application?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...