AR - Migrations - how to use a varchar as primary key instea


#1

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


#2

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.


#3

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 ?


#4

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


#5

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.


#6

Doh, I am sorry, I should have read that you mentioned Migrations.
Sorry, nothing to see from me.