One last Ruby question for tonight - Regarding UUID type

Started by Mike Christensenabout 15 years ago3 messagesgeneral
Jump to latest
#1Mike Christensen
mike@kitchenpc.com

My goal is to learn Ruby by porting one of my existing PG web
applications over to Rails.. However, my existing data heavily relies
on the UUID data type. I've noticed when I create a new model with
something like:

guidtest name:string value:uuid

And then do a rake:migrate, the CREATE TABLE that gets generated looks like:

CREATE TABLE guidtests
(
id serial NOT NULL,
"name" character varying(255),
created_at timestamp without time zone,
updated_at timestamp without time zone,
CONSTRAINT guidtests_pkey PRIMARY KEY (id)
)
...

In other words, it just ignores my "uuid" type. However, the views
and stuff do include this column so the page will crash when I load it
since the column doesn't exist in the DB.

Is there some special thing I have to do to use the uuid type in
ActiveRecord? Thanks!

#2Mike Christensen
mike@kitchenpc.com
In reply to: Mike Christensen (#1)
Re: One last Ruby question for tonight - Regarding UUID type

My goal is to learn Ruby by porting one of my existing PG web
applications over to Rails..  However, my existing data heavily relies
on the UUID data type.  I've noticed when I create a new model with
something like:

guidtest name:string value:uuid

And then do a rake:migrate, the CREATE TABLE that gets generated looks like:

CREATE TABLE guidtests
(
 id serial NOT NULL,
 "name" character varying(255),
 created_at timestamp without time zone,
 updated_at timestamp without time zone,
 CONSTRAINT guidtests_pkey PRIMARY KEY (id)
)
...

In other words, it just ignores my "uuid" type.  However, the views
and stuff do include this column so the page will crash when I load it
since the column doesn't exist in the DB.
Is there some special thing I have to do to use the uuid type in
ActiveRecord?  Thanks!

Update: If I manually add the column in using pgAdmin (as a uuid type
of course), the program actually runs (I can create new rows and
display data).. So RoR does support this type (probably gets
marshalled as a string??) but I guess the ActiveRecord schema
generation stuff just doesn't support uuid. Hmmm.

#3Jonathan S. Katz
jonathan.katz@excoventures.com
In reply to: Mike Christensen (#2)
Re: One last Ruby question for tonight - Regarding UUID type

Each database adapter in ActiveRecord sets up a mapping between ActiveRecord types and the native database types. If the type is not defined, it just defaults it as a string.

If you are using Rails, in one of your environment initializers, you can add the following code:

ActiveRecord::Base.connection.native_database_types[:uuid] = 'uuid' # this is using ActiveRecord 3.0.3, but also works with 2.3.8

Then, when you add the column type in the migration as "uuid", it should work.

The reverse mapping, that allows AR to use the UUID as a string is already handled:

https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L67

Jonathan

On Jan 28, 2011, at 9:28 PM, Mike Christensen wrote:

Show quoted text

My goal is to learn Ruby by porting one of my existing PG web
applications over to Rails.. However, my existing data heavily relies
on the UUID data type. I've noticed when I create a new model with
something like:

guidtest name:string value:uuid

And then do a rake:migrate, the CREATE TABLE that gets generated looks like:

CREATE TABLE guidtests
(
id serial NOT NULL,
"name" character varying(255),
created_at timestamp without time zone,
updated_at timestamp without time zone,
CONSTRAINT guidtests_pkey PRIMARY KEY (id)
)
...

In other words, it just ignores my "uuid" type. However, the views
and stuff do include this column so the page will crash when I load it
since the column doesn't exist in the DB.
Is there some special thing I have to do to use the uuid type in
ActiveRecord? Thanks!

Update: If I manually add the column in using pgAdmin (as a uuid type
of course), the program actually runs (I can create new rows and
display data).. So RoR does support this type (probably gets
marshalled as a string??) but I guess the ActiveRecord schema
generation stuff just doesn't support uuid. Hmmm.

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general