# Couldn't find SalesOrder without an ID

**URL:** <https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224>\
**Category:** Rails\
**Created:** [October 31, 2009, 7:15am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224 "2009-10-31T07:15:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vishnukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dbc845/32.png) [@vishnukumar](https://www.ruby-forum.com/u/vishnukumar)\
**Post date:** [October 31, 2009, 7:15am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224/1 "2009-10-31T07:15:35Z")

</div>

hi,

I am doing unit testing. I have a table as SalesOrder, where  
“ordernumber” is the primary key of the table. When I insert records in  
the table from unit testing, I got error like,

ActiveRecord::RecordNotFound: Couldn’t find SalesOrder without an ID

But, I have fixed the error by adding set\_primary\_key “ordernumber” in  
the model, and tested the unit test class and works well. But, If I  
insert record from the application as SalesOrder.save, I got the same  
error. If I comment the line set\_primary\_key in the model, then the  
application works fine, but unit testing fails with this error.

Is there any alternative way, where unit test and application accepts?

Thank you

---

<div class="post-metadata">

**Author:** ![vishnukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dbc845/32.png) [@vishnukumar](https://www.ruby-forum.com/u/vishnukumar)\
**Post date:** [November 2, 2009, 4:31am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224/2 "2009-11-02T04:31:45Z")

</div>

Vishnu,

What’s wrong with using “ID” as the primary key instead of  
“ordernumber”? I would strongly advise doing so; you will most likely  
save yourself (and others) current and future headaches by adopting  
this practice.

Best regards,  
Sebastian

On Oct 31, 4:15 pm, Vishnu F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![vishnukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dbc845/32.png) [@vishnukumar](https://www.ruby-forum.com/u/vishnukumar)\
**Post date:** [November 2, 2009, 8:43am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224/3 "2009-11-02T08:43:51Z")

</div>

Sebastian von Conrad wrote:

> Vishnu,
> 
> What’s wrong with using “ID” as the primary key instead of  
> “ordernumber”? I would strongly advise doing so; you will most likely  
> save yourself (and others) current and future headaches by adopting  
> this practice.
> 
> Best regards,  
> Sebastian
> 
> On Oct 31, 4:15ï¿½pm, Vishnu F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Sebastian,

Thank you for your reply. Its right to keep “ID” as primary key. But, my  
client requirement is to keep the “ordernumber” as primary key. So, I am  
trying to fix with this.

Thank you

---

<div class="post-metadata">

**Author:** ![vishnukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dbc845/32.png) [@vishnukumar](https://www.ruby-forum.com/u/vishnukumar)\
**Post date:** [November 2, 2009, 9:46am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224/4 "2009-11-02T09:46:26Z")

</div>

Hi Vishnu

In the normal case the procedure is like

## create\_table :sales\_orders, :id =\> false do |t| t.integer :ordernumber, :primary =\> true

end

Now in the class SalesOrder you can define like  
set\_primary\_key “ordernumber”  
And also you have to write a before\_create call back to set the  
ordernumber by some means to make it uniq  
self.ordernumber = getuniqnumber

```
 Is what u did so far like above?

    If it is a legacy db you can set like

```

set\_table\_name , set\_primary\_key, set\_sequence\_name …

Sijo

---

<div class="post-metadata">

**Author:** ![vishnukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dbc845/32.png) [@vishnukumar](https://www.ruby-forum.com/u/vishnukumar)\
**Post date:** [November 2, 2009, 11:24am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224/5 "2009-11-02T11:24:41Z")

</div>

Sijo k g wrote:

> Hi Vishnu
> 
> In the normal case the procedure is like
> 
> ## create\_table :sales\_orders, :id =\> false do |t| t.integer :ordernumber, :primary =\> true
> 
> end
> 
> Now in the class SalesOrder you can define like  
> set\_primary\_key “ordernumber”  
> And also you have to write a before\_create call back to set the  
> ordernumber by some means to make it uniq  
> self.ordernumber = getuniqnumber
> 
> ```
> Is what u did so far like above?
> 
> If it is a legacy db you can set like
> 
> ```
> 
> set\_table\_name , set\_primary\_key, set\_sequence\_name …
> 
> Sijo

hi Sijo,

I have did the same as you said as set\_primary\_key â€œordernumberâ€ in the  
model class as before. I am creating model class object from the unit  
test class, by getting values from the fixtures. The object creation is  
fine, when I mention the statement set\_primary\_key ‘ordernumber’ in the  
model class and running the unit test class with no errors. But, When I  
run the rails application normally (not from unit test class), it  
doesn’t create object, it throws exception, when the set\_primary\_key is  
present. If I comment the line, then, the application runs without  
error. Error as

ActiveRecord::RecordNotFound: Couldn’t find SalesOrder without an ID

Thank you

---

<div class="post-metadata">

**Author:** ![vishnukumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/dbc845/32.png) [@vishnukumar](https://www.ruby-forum.com/u/vishnukumar)\
**Post date:** [November 2, 2009, 10:00am UTC](https://www.ruby-forum.com/t/couldnt-find-salesorder-without-an-id/178224/6 "2009-11-02T10:00:41Z")

</div>

On Sun, Nov 1, 2009 at 11:43 PM, Vishnu F. \<  
removed\_email\_address@domain.invalid\> wrote:

> > Sebastian

The id field is really used for database record management and it tends  
to  
be a read-only field after the model instance is created. What happens  
when  
the format of the order number changes? You might want to read and  
fully  
understand the consequences by reading section 18.3 of AWDwR 3ed.

Good luck,

-Conrad
