# Changing primary.. Rails/mysql

**URL:** <https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003>\
**Category:** Rails\
**Created:** [December 6, 2008, 2:14am UTC](https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003 "2008-12-06T02:14:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![den1jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@den1jay](https://www.ruby-forum.com/u/den1jay)\
**Post date:** [December 6, 2008, 2:14am UTC](https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003/1 "2008-12-06T02:14:58Z")

</div>

I am using Rails 2.0.2 with mysql database  
Migrate sets the primary key to id.  
Is it possible to change it to another column?  
If yes, how?  
Thanks for your help

Cypray.

---

<div class="post-metadata">

**Author:** ![den1jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@den1jay](https://www.ruby-forum.com/u/den1jay)\
**Post date:** [December 6, 2008, 6:22am UTC](https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003/2 "2008-12-06T06:22:08Z")

</div>

First thing I would ask is “why?” What is the reason behind the  
change?

---

<div class="post-metadata">

**Author:** ![den1jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@den1jay](https://www.ruby-forum.com/u/den1jay)\
**Post date:** [December 6, 2008, 7:50am UTC](https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003/3 "2008-12-06T07:50:05Z")

</div>

Well, I am having trouble with this query:

class Author \< ActiveRecord::Base

has\_many :books

def author\_age  
@results = Author.find :all, :conditions =\> [“age = ?”,  
params[:authors]]  
end  
end

I have this on view\authors\show.html

\<% @results.each do |result| %\>

| 

_ **Author Name** _

 | 

_ **Age** _

 |
| \<%=h @result.name %\> | \<%=h @result.age %\> |

I am getting this error: “You have a nil object when you didn’t  
expect it!”  
The log shows “NULL” for the selected age whose value should be passed  
in as the condition for the query.

So, I wanted to change the primary key just to see if it might make any  
difference. I don’t know what is wrong with the query.  
In SQL, the query is like this:  
SELECT name, age FROM author WHERE age = “the user selected age”

Cypray.

Bobnation wrote:

> First thing I would ask is “why?” What is the reason behind the  
> change?

---

<div class="post-metadata">

**Author:** ![den1jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@den1jay](https://www.ruby-forum.com/u/den1jay)\
**Post date:** [December 6, 2008, 8:16am UTC](https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003/4 "2008-12-06T08:16:42Z")

</div>

On Fri, Dec 5, 2008 at 10:50 PM, Jay M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> ```
> end
> 
> ```
> 
> \<% @results.each do |result| %\>
> 
> \<%=h @result.name %\> \<%=h @result.age %\>

Hi, in the above HTML, you should be using result instead of @result.  
For  
example,

 \<%=h result.name %\> \<%=h result.age %\>

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![den1jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eb8c5e/32.png) [@den1jay](https://www.ruby-forum.com/u/den1jay)\
**Post date:** [December 6, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/changing-primary-rails-mysql/155003/5 "2008-12-06T16:30:56Z")

</div>

On Fri, Dec 5, 2008 at 10:50 PM, Jay M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def author\_age  
> @results = Author.find :all, :conditions =\> [“age = ?”,  
> params[:authors]]  
> end

> I am getting this error: “You have a nil object when you didn’t  
> expect it!”  
> The log shows “NULL” for the selected age whose value should be passed  
> in as the condition for the query.
> 
> So, I wanted to change the primary key just to see if it might make any  
> difference. I don’t know what is wrong with the query.

Change the primary key? Why not repaint your bedroom? Maybe  
put new tires on the car? 🙂

I already told you in another thread: params[:authors] IS NOT the  
value you think it is. You are NOT PASSING a valid value for “age”  
so of course the result set is empty, giving you a nil @results.

Your form doesn’t match up with your code. That’s the problem.

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid
