Forum: Ruby on Rails AR - Migrations - how to use a varchar as primary key instea

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Thibaut Barrère (Guest)
on 2006-03-13 00:02
(Received via mailing list)
Hi

I'd like to have a varchar primary key named id instead of the default
int
id. Does anyone knows how to specify this from migrations ?

Should I also specify something in the model class ?

thanks!

Thibaut
Corey L. (Guest)
on 2006-03-13 09:39
(Received via mailing list)
Ya probably just...do it. You probably also want some kind of
iterator/sequence/generator/trigger in place to supply new values
automatically on insert.

What you should probably also do is create a table that stores the
last value used, so as to avoid the "select max(id) from mytable..."
brute force logic as well as any transaction issues, things that
sequences/generators do for you already in a transaction-safe manner.
Thibaut Barrère (Guest)
on 2006-03-13 13:07
(Received via mailing list)
Hi!

well when I add a column under migration, the type can be either
:string,
:integer, or :primary_key. I can't seem to find how to have it defined
as
both a :string and a :primary_key. Or should I add custom sql ?
Anthony G. (Guest)
on 2006-03-13 14:35
Thibaut Barrère wrote:
> Hi
>
> I'd like to have a varchar primary key named id instead of the default
> int
> id. Does anyone knows how to specify this from migrations ?

The Agile book suggest setting up a two primary ids - the first a new
one : int auto incremented and a second one your legacy pk

_tony
Briggs (Guest)
on 2006-03-14 02:18
(Received via mailing list)
Thibaut Barrère wrote:
> Hi
can you just call the primary_key() method in your model class (your
subclass of ActiveRecord::Base)?

The RDocs:

primary_key()
Defines the primary key field ? can be overridden in subclasses.
Overwriting will negate any effect of the primary_key_prefix_type
setting, though.
Briggs (Guest)
on 2006-03-14 02:30
(Received via mailing list)
Doh, I am sorry, I should have read that you mentioned Migrations.
Sorry, nothing to see from me.
This topic is locked and can not be replied to.