[Rails] Re: The need of IDs in ActiveRecord

Eric Anderson eric at afaik.us
Wed Jan 12 00:50:42 GMT 2005


Joe Van Dyk wrote:
> What is a child table?

CREATE TABLE people (
	id INT PRIMARY KEY AUTO_INCREMENT,
	first_name VARCHAR(30) NOT NULL,
	last_name VARCHAR(40) NOT NULL
)

CREATE TABLE employees (
	person INT UNIQUE NOT NULL,
	department VARCHAR(255) NOT NULL,
	boss INT NOT NULL,
	FOREIGN KEY (person) REFERENCES people.id
	FOREIGN KEY (boss) REFERENCES people.id
)

CREATE TABLE clients (
	person INT UNIQUE NOT NULL,
	company VARCHAR(255) NOT NULL,
	FOREIGN KEY (person) REFERENCES people.id
)

In this case employee and client are child tables of people. For every 
record in employees or clients you will have a record in people, but you 
may have records in people that are not employees or clients (they could 
be vendors, or something else entirely).

So:

        people
       /     \
      /       \
     /         \
employees  clients

Eric
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 187 bytes
Desc: OpenPGP digital signature
Url : http://one.textdrive.com/pipermail/rails/attachments/20050111/646b1603/signature.bin


More information about the Rails mailing list