# Yet another primary key and migrations question

**URL:** <https://www.ruby-forum.com/t/yet-another-primary-key-and-migrations-question/123429>\
**Category:** Rails\
**Created:** [December 17, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/yet-another-primary-key-and-migrations-question/123429 "2007-12-17T18:01:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [December 17, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/yet-another-primary-key-and-migrations-question/123429/1 "2007-12-17T18:01:23Z")

</div>

I’ve read the messages concerning this issue, but I haven’t come to a  
conclusion.

I have a legacy db with data I have to import to my new app, as it is.  
So I can’t use the auto-increment “id” my migrations generate.

I’ve done the following:  
create\_table(:results, :id =\> false, :primary\_key =\> ‘code’ ) do |t|  
t.column :code, :integer

But, in my SQL server, “code” does not appear as primary key.

So, what can I do to make so?

And if I use the set\_primary\_key in the model, it does not solve  
anything… I mean, this primary key in SQL server will not be set to  
primary key (so I think I won’t use the migrations at all…)

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [December 17, 2007, 6:34pm UTC](https://www.ruby-forum.com/t/yet-another-primary-key-and-migrations-question/123429/2 "2007-12-17T18:34:31Z")

</div>

I’ve done the following:

create\_table(:interpretations, :id=\> false) do |t|  
t.column :code, :integer  
t.column :descripcion, :string, :limit =\> 80  
end  
execute “ALTER TABLE `interpretations` ADD PRIMARY KEY (`code`)”

But now I have to state that primary key is “code” also in the model. So  
this is not DRY anymore.

---

<div class="post-metadata">

**Author:** ![damaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/2bfe46/32.png) [@damaris](https://www.ruby-forum.com/u/damaris)\
**Post date:** [December 18, 2007, 12:50am UTC](https://www.ruby-forum.com/t/yet-another-primary-key-and-migrations-question/123429/3 "2007-12-18T00:50:20Z")

</div>

On Mon, 17 Dec 2007 18:34:31 +0100, Damaris F. wrote:

> ```
> execute "ALTER TABLE `interpretations` ADD PRIMARY KEY (`code`)"
> 
> ```
> 
> But now I have to state that primary key is “code” also in the model. So  
> this is not DRY anymore.

Wouldn’t the lingo be:

“how to DRY out the above code?” 😉

-Thufir
