# Tests failing for models with string primary key

**URL:** <https://www.ruby-forum.com/t/tests-failing-for-models-with-string-primary-key/158648>\
**Category:** Rails\
**Created:** [January 26, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/tests-failing-for-models-with-string-primary-key/158648 "2009-01-26T13:02:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaczheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/edb3f5/32.png) [@zaczheng](https://www.ruby-forum.com/u/zaczheng)\
**Post date:** [January 26, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/tests-failing-for-models-with-string-primary-key/158648/1 "2009-01-26T13:02:39Z")

</div>

Hi,

My tests(rspec) are failing on models that has a String primary key.  
Rails wrongly creates the test table with the primary key in Integer  
format. As a result, the fixtures can not be imported, nor does  
relationships match up.

The database is imported and needs to match with an external database,  
so there is no possibly of changing the primary key to integer.

Does anyone know how to force Rails to recreate the test tables to  
respect string primary keys?

Another poster also experienced this problem,  
[Tests failing on table with primary key as string - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/90740#new). But unfortunately he didn’t  
get a reply.

Much thanks,  
Zac

---

<div class="post-metadata">

**Author:** ![zaczheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/edb3f5/32.png) [@zaczheng](https://www.ruby-forum.com/u/zaczheng)\
**Post date:** [February 22, 2009, 11:19pm UTC](https://www.ruby-forum.com/t/tests-failing-for-models-with-string-primary-key/158648/2 "2009-02-22T23:19:36Z")

</div>

Zac Z. wrote:

> Hi,
> 
> My tests(rspec) are failing on models that has a String primary key.  
> Rails wrongly creates the test table with the primary key in Integer  
> format. As a result, the fixtures can not be imported, nor does  
> relationships match up.
> 
> The database is imported and needs to match with an external database,  
> so there is no possibly of changing the primary key to integer.
> 
> Does anyone know how to force Rails to recreate the test tables to  
> respect string primary keys?

This fix this turned out to be easy.

Schema.rb is run to create the test tables. I edited my copy to the  
following:

create\_table “countries”, :force =\> true, :id =\> false do |t|  
t.column “locode”

# other columns

end

# Manually assign ‘locode’ as primary key

execute “ALTER TABLE `countries` ADD PRIMARY KEY (`locode`);”

Hey Presto! My specs that use countries are now running.

Thanks to [Non integer primary key - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/113935#new)

Zac

---

<div class="post-metadata">

**Author:** ![zaczheng](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/edb3f5/32.png) [@zaczheng](https://www.ruby-forum.com/u/zaczheng)\
**Post date:** [February 22, 2009, 11:23pm UTC](https://www.ruby-forum.com/t/tests-failing-for-models-with-string-primary-key/158648/3 "2009-02-22T23:23:57Z")

</div>

I’ve made $109 on this free sportsbetting site, and my friend currently  
has over  
$200!..

You start with 10 cents and when you get to $20 you can cash out or keep  
going!  
Check it out…

> **[Free Sports Betting | Play at our Free Sportsbook - No Deposit | CentSports](http://www.centsports.com/?opcode=289473)**
>
> CentSports.com is the most innovative free fake money sports wagering site with no deposit. If you are looking for free sports betting website to bet on sports risk-free then your search ends here. Join us today and start winning money in our Free...
