# Adding primary\_key to join table using migrations

**URL:** <https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648>\
**Category:** Rails\
**Created:** [April 2, 2006, 4:19am UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648 "2006-04-02T04:19:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [April 2, 2006, 4:19am UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648/1 "2006-04-02T04:19:08Z")

</div>

Hi list,

My tag\_question\_user join table is not currently a full-blown model. It  
doesn’t have a primary key - just tag\_id, question\_id, and user\_id.

I now want to add a primary key to this model (because I want to add a  
“description” column as well so users can describe the tag.

I’m not sure how to add this using migrations.

Here is what I have:  
class AddNotesToTags \< ActiveRecord::Migration  
def self.up  
add\_column :tags\_questions\_users, “id”, :primary\_key  
add\_column :tags\_questions\_users, “description”, :string, :limit  
=\>  
80, :null =\> false  
end

def self.down  
remove\_column :tags\_questions\_users, “id”  
remove\_column :tags\_questions\_users, “description”  
end  
end

That’s giving me this error:

Mysql::Error: #42000You have an error in your SQL syntax; check the  
manual  
that  
corresponds to your MySQL server version for the right syntax to use  
near ‘’  
at  
line 1: ALTER TABLE tags\_questions\_users ADD id

Any suggestions?

Thanks,

Steve  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [July 6, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648/2 "2006-07-06T19:53:34Z")

</div>

Hi Steve,

Did you ever find a solution to this? We’re having the same problem.  
As far as I can see the mysql adapter isn’t set up to handle the  
:primary\_key type.

I found a patch for Postgre:

[http://dev.rubyonrails.org/ticket/3735](http://dev.rubyonrails.org/ticket/3735)

Looks like this might be fixed in 1.2

CHEERS\> SAM

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [July 6, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648/3 "2006-07-06T20:16:04Z")

</div>

I’m also looking for some way to drop a primary key, but my first  
attempt:

```
remove_index :workspaces_discussions, :primary_key

```

has failed miserably:

Mysql::Error: Can’t DROP ‘workspaces\_discussions\_primary\_key\_index’;  
check that  
column/key exists: DROP INDEX workspaces\_discussions\_primary\_key\_index  
ON worksp  
aces\_discussions

Many thanks for any suggestions.

CHEERS\> SAM

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [July 6, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648/4 "2006-07-06T20:22:44Z")

</div>

My table primary key is “DATE” and not “ID”

now When I use mymodel.find(“mydate”) it’s returns that my table does  
not have a column “id”  
ok…

now how to search by DATE and not “id” ?

tks.

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [July 6, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648/5 "2006-07-06T21:11:22Z")

</div>

Hi Fernando,

Fernando wrote:

> My table primary key is “DATE” and not “ID”
> 
> now When I use mymodel.find(“mydate”) it’s returns that my table does not  
> have a column “id”  
> ok…
> 
> now how to search by DATE and not “id” ?

Rails is built on top of ActiveRecord which assumes the primary key is a  
“meaningless” integer. Simply add a date field (I don’t think you can  
name  
it just ‘date’, but you should check the wiki for reserved / magic  
words).  
In Rails, assuming your date column is named ‘mydate’, you’ll use

mymodel.find(:first, :conditions =\> [“mydate = ?”, mydate])

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![steveoco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/db5fbb/32.png) [@steveoco](https://www.ruby-forum.com/u/steveoco)\
**Post date:** [July 6, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/adding-primary-key-to-join-table-using-migrations/56648/6 "2006-07-06T23:08:39Z")

</div>

On 7/6/06, Fernando [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My table primary key is “DATE” and not “ID”
> 
> now When I use mymodel.find(“mydate”) it’s returns that my table does  
> not have a column “id”  
> ok…
> 
> now how to search by DATE and not “id” ?

You need to override AR default with set\_primary\_key ‘date’ in your  
Model. AR assumes the primary key is called “id”.  
If you are not using legacy schemas, best practice is however using  
Integer primary keys.  
Cheers.
