Topic: Primary keys

Hello,
I am extremely confused about how rails handles primary keys when the migration tool is not used. What if we just create a primary key column called "id" using sql? Rails doesnt seem to auto-increment the id even though the column is called "id". If I understand correctly, rails automatically increment primary keys if the column is called "id", if not, we have to specifically tell rails what the primary key column is. Any direction will be greatly appreciated.

Thanks

Re: Primary keys

Rails itself does not do the auto increment, it's handled by the database.  For example if you're using mysql you would do this.

CREATE TABLE animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
);

http://dev.mysql.com/doc/refman/5.0/en/ … ement.html

I'm sure you have your reasons, but I HIGHLY recommend using migrations; they're truly awesome.

Last edited by thabenksta (2006-11-10 01:15:27)

Re: Primary keys

Well, when I was first starting with Rails I had absolutely NO idea how they worked and was quite terrified of them.
Now I can't believe I got by for so many years without them. *curses PHP* haha

Re: Primary keys

Thanks a lot for the reply!