# Foreign keys not showing!

**URL:** <https://www.ruby-forum.com/t/foreign-keys-not-showing/93557>\
**Category:** Rails\
**Created:** [April 4, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-showing/93557 "2006-04-04T19:35:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bealach\_Na\_Bo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e19b73/32.png) [@Bealach\_Na\_Bo](https://www.ruby-forum.com/u/Bealach_Na_Bo)\
**Post date:** [April 4, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-showing/93557/1 "2006-04-04T19:35:29Z")

</div>

I’m new at this so please be patient.

I have a very simple schema consisting of a around 10 tables in  
Postgresql. The aim is to provide a system for PC administrators to  
keep track of PC’s dotted around various venues (shops, libraries  
etc.). The sort of info I want to store is PC specs, PC network specs,  
PC ownership specs, PC location specs and contact person details. So I  
end up with a few tables associated with one another through foreign  
keys and join tables. Two of the tables, for example, are created  
with (nodes are PC’s):

Create table “shops”  
(  
“id” Serial NOT NULL,  
“name” Varchar(20) NOT NULL,  
“street” Varchar(20),  
“zip” Varchar(20),  
“city” Varchar(20),  
“phone” Varchar(20),  
“contact” Varchar(20),  
“hours” Varchar(20),  
primary key (“id”)  
) With Oids;

Create table “nodes”  
(  
“id” Serial NOT NULL,  
“shop\_id” Integer NOT NULL,  
“name” Varchar(20),  
“ip” Varchar,  
primary key (“id”)  
) With Oids;

Alter table “nodes” add foreign key (“shop\_id”) references “shops”  
(“id”)  
on update restrict on delete restrict;

Now I want rails to present a form for registering a new node - that is  
node  
specs and which shop it belongs to. So I  
run

$\> ruby script/generate scaffold Node Admin  
and  
$\> ruby script/generate model Shop

and edited /app/models/node.rb to read

class Node \< ActiveRecord::Base  
belongs\_to :shop  
end

But when I go to [http://localhost:3000/admin/new](http://localhost:3000/admin/new)  
the shop\_id field is not one of the fields.

I’ve obviously not understood how much automation can be expected from  
Rails. Any comments and pointers would be much appreciated.

Warm regards,

Bealach

---

<div class="post-metadata">

**Author:** ![Bealach\_Na\_Bo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e19b73/32.png) [@Bealach\_Na\_Bo](https://www.ruby-forum.com/u/Bealach_Na_Bo)\
**Post date:** [April 4, 2006, 7:38pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-showing/93557/2 "2006-04-04T19:38:31Z")

</div>

You can always edit the .rhtml files to include any fields that are not  
showing. By default the primary fields and foreign key aren’t shown  
when you scaffold a table.

---

<div class="post-metadata">

**Author:** ![Bealach\_Na\_Bo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e19b73/32.png) [@Bealach\_Na\_Bo](https://www.ruby-forum.com/u/Bealach_Na_Bo)\
**Post date:** [April 4, 2006, 8:09pm UTC](https://www.ruby-forum.com/t/foreign-keys-not-showing/93557/3 "2006-04-04T20:09:16Z")

</div>

Thanks Charlie. I guess I was hoping to get away with a bit less work. I  
don’t suppose there is a way of telling Rails to create the scaffold for  
things like “create shop if it does not exit” and then write the code  
for  
taking the user to a “shop registry” page.

Bealach
