ActiveRecord Migrations, without autonumbered PK's

I love using ActiveRecord Migrations to build tables.

Sometimes, I don’t want to use autogenerated PK’s - I want to set them
automatically (why? I’m importing read only data from a large list of
medications, and want to use the PK’s assigned by the medication
research company…). Is there anyway to do this using Migrations? I
know that I don’t need to set a PK, but I want to set a PK - just leave
it manual.

create_table :foo, :id => false do |t|
end

I think you’d be better off creating a unique
ID on the research company’s data, in addition
to Rail’s ID, but if you want the extra pain,
go for it.


– Tom M.

On 6/30/06, Tom M. [email protected] wrote:

create_table :foo, :id => false do |t|
end

I think you’d be better off creating a unique
ID on the research company’s data, in addition
to Rail’s ID, but if you want the extra pain,
go for it.

+1 on Tom’s comment. External Primary Keys should be a separate
column in your table. It’s not the only way to do it, but it’s by
far the least painful. Everything works much more seamlessly when you
have your autonumber “id” field and a separate “external_key” (or
whatever) field. It’s just as easy to do the import and much less
painful to do anything else.

-Curtis

Tom M. wrote:

create_table :foo, :id => false do |t|
end

I do want a PK, just not autonumber.
Using the method primary_key() makes it autonumber.

I think you’d be better off creating a unique
ID on the research company’s data, in addition
to Rail’s ID, but if you want the extra pain,
go for it.

I certainly agree that ActiveRecord is happiest this way. Let me
describe a bit more the situation, and maybe you’ll see why I want to do
differently, nonetheless:

The research consists of huge numbers of files, with huge numbers of
records, referencing eachother through integer FK’s, in text delimited
formats.

Using random PK’s makes loading the references a nightmare. The load
procedure needs to do all the work manually. If you make a mistake,
it’s very hard to sort out what happened. Adding new tables (purchased
later) is very hard.

The data is read only under normal operations - all we do is load it -
so I’d rather make the load easy.

Robert J. <[email protected]…> writes:

I love using ActiveRecord Migrations to build tables.

Sometimes, I don’t want to use autogenerated PK’s - I want to set them
automatically (why? I’m importing read only data from a large list of
medications, and want to use the PK’s assigned by the medication
research company…). Is there anyway to do this using Migrations? I
know that I don’t need to set a PK, but I want to set a PK - just leave
it manual.

It’s a little clunky, but you can do this:


create_table :my_table, :primary_key => :guid do |t|
end
change_column :my_table, :guid, :string, :limit => 32

The first step creates an autonumber column called guid and the second
step
changes the type of that field.

hth,
-damon
http://damonclinkscales.com/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs