# ActiveRecord Migrations, without autonumbered PK's

**URL:** <https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650>\
**Category:** Rails\
**Created:** [June 30, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650 "2006-06-30T15:31:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [June 30, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650/1 "2006-06-30T15:31:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [June 30, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650/2 "2006-06-30T20:44:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [June 30, 2006, 9:12pm UTC](https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650/3 "2006-06-30T21:12:25Z")

</div>

On 6/30/06, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [June 30, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650/4 "2006-06-30T22:20:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [July 1, 2006, 5:56am UTC](https://www.ruby-forum.com/t/activerecord-migrations-without-autonumbered-pks/96650/5 "2006-07-01T05:56:58Z")

</div>

Robert J. \<srobertjames@…\> 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

> **[Damon Clinkscales](https://damonc.onuniverse.com/)**
>
> Damon Clinkscales
