# Why is rails using DB objects that don't exist?

**URL:** <https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377>\
**Category:** Rails\
**Created:** [June 25, 2006, 1:24am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377 "2006-06-25T01:24:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fburleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@fburleigh](https://www.ruby-forum.com/u/fburleigh)\
**Post date:** [June 25, 2006, 1:24am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377/1 "2006-06-25T01:24:17Z")

</div>

I admit it–I’m newb right now. But only to rails, ruby and friends.  
I’m walking through a “recipe” tutorial but have been stuck for hours;  
it’s time to ask for help. On Windows I’ve got the latest ruby, rails,  
and webrick; I’ve got the Oracle OCI “thing” to talk to our Oracle 10g  
database, I’ve got Slick Edit and a command line.

I’ve created a recipes table, a sequence to feed its ID primary key  
column, and a trigger that gets fired before record creation. I can use  
sql at a command line in Oracle’s sqlplus to add records. No sweat.  
But in the browser I get:

ActiveRecord::StatementInvalid in RecipeController#create

OCIError: ORA-02289: sequence does not exist: select recipes\_seq.nextval  
id from dual

I did once have a sequence with that name, but that’s long gone. In  
fact I’ve dropped and recreated the table, sequence and trigger several  
times with new names (except I continue using “recipes” for the table  
name).

I’ve closed and opened and closed firefox. I’ve restarted webrick.

I’m having trouble imagining what’s up–because I believe this little  
ruby app isn’t looking into the details of how an incrementing field  
works. There are clearly remnants somewhere, I just can’t find them.

WTF?!

Thanks anyone for getting me back on track.

---

<div class="post-metadata">

**Author:** ![fburleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@fburleigh](https://www.ruby-forum.com/u/fburleigh)\
**Post date:** [June 25, 2006, 1:34am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377/2 "2006-06-25T01:34:39Z")

</div>

Although I’m not using oracle, I suspect that rails is using  
standardized names to generate the sql to generate the recipe PK.

…so I’d suggest creating a sequence “recipes\_seq” - rails likely  
takes table name + ‘\_seq’, and runs your quoated sql to obtain a pk  
before doing an insert into recipes.

Jodi

---

<div class="post-metadata">

**Author:** ![fburleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@fburleigh](https://www.ruby-forum.com/u/fburleigh)\
**Post date:** [June 25, 2006, 2:38am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377/3 "2006-06-25T02:38:25Z")

</div>

Jodi S. wrote:

> Although I’m not using oracle, I suspect that rails is using  
> standardized names to generate the sql to generate the recipe PK.
> 
> …so I’d suggest creating a sequence “recipes\_seq” - rails likely  
> takes table name + ‘\_seq’, and runs your quoated sql to obtain a pk  
> before doing an insert into recipes.
> 
> Jodi

That makes outstanding sense. I have written pretty much _no_ code so  
I’m clueless what the sql looks like. Is there a means for me to see  
that? That would make this less magical, but also more comprehensible.  
😉

And thanks for your reply.

---

<div class="post-metadata">

**Author:** ![fburleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@fburleigh](https://www.ruby-forum.com/u/fburleigh)\
**Post date:** [June 25, 2006, 2:53am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377/4 "2006-06-25T02:53:03Z")

</div>

Frank Burleigh wrote:

> Jodi S. wrote:
> 
> > Although I’m not using oracle, I suspect that rails is using  
> > standardized names to generate the sql to generate the recipe PK.
> > 
> > …so I’d suggest creating a sequence “recipes\_seq” - rails likely  
> > takes table name + ‘\_seq’, and runs your quoated sql to obtain a pk  
> > before doing an insert into recipes.
> > 
> > Jodi
> 
> That makes outstanding sense. I have written pretty much _no_ code so  
> I’m clueless what the sql looks like. Is there a means for me to see  
> that? That would make this less magical, but also more comprehensible.  
> 😉
> 
> And thanks for your reply.

[http://api.rubyonrails.com/](http://api.rubyonrails.com/)

Rails, by default, guesses at the name of the sequence, as described by  
the previous replier. To set the name of the sequence

class Recipe\< ActiveRecord::Base  
set\_sequence\_name “recipeseq” # default would have been “recipe\_seq”  
end

Rails uses conventions for a lot of these things, such as table names,  
column names, sequences, etc. If you name your things like Rails  
expects, you don’t ahve to do anything else. However, if you don’t,  
that’s not a problem. You just have to tell rails what they are called.

For a class Recipe, rails assumes the tables name is loads. For all  
tables, it assumed the primary key is a column called id. However, you  
can tell it differently.

Class Recipe \< ActiveRecord::Base  
set\_table\_name “recipe”  
set\_primary\_key “recipe”  
end

If your table was recipe with a primary key called recipe, you can just  
tell it that. That’s what I do to use ActiveRecord with the database  
for a project at work, where the schema is 10 years old and  
pre-existing.

---

<div class="post-metadata">

**Author:** ![fburleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@fburleigh](https://www.ruby-forum.com/u/fburleigh)\
**Post date:** [June 25, 2006, 3:24am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377/5 "2006-06-25T03:24:58Z")

</div>

you can see the sql, as it runs, in the log/development.log. (useful  
for tuning as well)

As noted, the books are really worthwhile. AWD, recipes, etc.

have fun!  
Jodi

---

<div class="post-metadata">

**Author:** ![fburleigh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@fburleigh](https://www.ruby-forum.com/u/fburleigh)\
**Post date:** [June 25, 2006, 2:55am UTC](https://www.ruby-forum.com/t/why-is-rails-using-db-objects-that-dont-exist/64377/6 "2006-06-25T02:55:35Z")

</div>

Frank Burleigh wrote:

> Jodi S. wrote:
> 
> > Although I’m not using oracle, I suspect that rails is using  
> > standardized names to generate the sql to generate the recipe PK.
> > 
> > …so I’d suggest creating a sequence “recipes\_seq” - rails likely  
> > takes table name + ‘\_seq’, and runs your quoated sql to obtain a pk  
> > before doing an insert into recipes.
> > 
> > Jodi
> 
> That makes outstanding sense. I have written pretty much _no_ code so  
> I’m clueless what the sql looks like. Is there a means for me to see  
> that? That would make this less magical, but also more comprehensible.  
> 😉
> 
> And thanks for your reply.

Agile Web D. with Rails is the gold standard for Rails books.  
It’s absolutely outstanding, and in addition to being tremendously  
informative, it’s also fairly interesting to boot. If you are doing any  
Rails work at all, it’s a non-optional purchase.

[http://www.pragmaticprogrammer.com/titles/rails/index.html](http://www.pragmaticprogrammer.com/titles/rails/index.html)
