# Migrate postgresql database to uuid

**URL:** <https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127>\
**Category:** Rails\
**Created:** [October 25, 2013, 3:19pm UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127 "2013-10-25T15:19:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [October 25, 2013, 3:19pm UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/1 "2013-10-25T15:19:32Z")

</div>

Hi all,

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.

Could you please tell me some example,how to do this.

kingston.s

---

<div class="post-metadata">

**Author:** ![kingston\_jenorish.s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kingston\_jenorish.s](https://www.ruby-forum.com/u/kingston_jenorish.s)\
**Post date:** [October 25, 2013, 3:45pm UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/2 "2013-10-25T15:45:15Z")

</div>

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.

Atenciosamente,

_Carlos Figueiredo_

On Fri, Oct 25, 2013 at 11:17 AM, kingston jenorish.s \<

---

<div class="post-metadata">

**Author:** ![kingston\_jenorish.s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kingston\_jenorish.s](https://www.ruby-forum.com/u/kingston_jenorish.s)\
**Post date:** [October 27, 2013, 5:29am UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/3 "2013-10-27T05:29:26Z")

</div>

You might need to write a migration or rake task for this purpose.

Migration may look like this.

class MigrateData \< ActiveRecord::Migration  
def change  
get\_all\_models.each do |model|  
model.where(’’).find\_each do |model\_instance|  
model\_instance.update\_attributes id:  
UUIDTools::UUID.random\_create.to\_s  
end  
end  
end

def get\_all\_models  
Module.constants.select do |constant\_name|  
constant = eval constant\_name  
if not constant.nil? and constant.is\_a? Class and  
constant.superclass == ActiveRecord::Base  
constant  
end  
end  
endend

This migration will iterate over all the models in your app and generate  
UUID for each single instance.

---

<div class="post-metadata">

**Author:** ![kingston\_jenorish.s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kingston\_jenorish.s](https://www.ruby-forum.com/u/kingston_jenorish.s)\
**Post date:** [October 28, 2013, 6:31am UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/4 "2013-10-28T06:31:21Z")

</div>

I could not understand that what do you mean by single instance  
migration  
with relation. Can you please elaborate more?

---

<div class="post-metadata">

**Author:** ![kingston\_jenorish.s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kingston\_jenorish.s](https://www.ruby-forum.com/u/kingston_jenorish.s)\
**Post date:** [October 28, 2013, 5:38am UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/5 "2013-10-28T05:38:12Z")

</div>

For single instance migration we could do like this,what to be done if i  
want to migrate single instance with the relation?

---

<div class="post-metadata">

**Author:** ![kingston\_jenorish.s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kingston\_jenorish.s](https://www.ruby-forum.com/u/kingston_jenorish.s)\
**Post date:** [October 28, 2013, 6:59am UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/6 "2013-10-28T06:59:59Z")

</div>

I have model called user.

user.rb

class User \< ActiveRecord::Base

has\_many :sites,:through=\>:site\_user\_roles  
belongs\_to :country  
has\_many :payments  
has\_and\_belongs\_to\_many :events

end

like above i am having lots of association,now i want to migrate all the  
relation to uuid.

---

<div class="post-metadata">

**Author:** ![kingston\_jenorish.s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/0ea827/32.png) [@kingston\_jenorish.s](https://www.ruby-forum.com/u/kingston_jenorish.s)\
**Post date:** [October 29, 2013, 7:08am UTC](https://www.ruby-forum.com/t/migrate-postgresql-database-to-uuid/237127/7 "2013-10-29T07:08:58Z")

</div>

By looking at your code, I assume that you have these models in your  
application. Site, Country,Payment, SiteUserRole and Event. If that’s  
true  
then you don’t have to worry about that. The migration, that I shared  
before, will iterate through all these models and ID in every database  
table will be replaced with generated UUID.
