ActiveRecord and incrementing the key

  • Thread starter Abder-Rahman Ali
  • Start date
A

Abder-Rahman Ali

In "The Rails Way" book, the following is mentiond in Chapter
(6):Working with ActiveRecord

"Also by convention, ActiveRecord will expect an id column to use as
primary key. It should be an integer and [[incrementing of the key
should be managed automatically by the database server when creating new
records]]."

How does the database server manage the incrementing of the key? What is
meant here by the database server is what manages the incrementing of
the key?

Is there an example that may make this point more clear. For example,
the following "database server" increments......

Thanks.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

On Sat, Sep 4, 2010 at 10:17 AM, Abder-Rahman Ali <
In "The Rails Way" book, the following is mentiond in Chapter
(6):Working with ActiveRecord

"Also by convention, ActiveRecord will expect an id column to use as
primary key. It should be an integer and [[incrementing of the key
should be managed automatically by the database server when creating new
records]]."

How does the database server manage the incrementing of the key? What is
meant here by the database server is what manages the incrementing of
the key?

Is there an example that may make this point more clear. For example,
the following "database server" increments......

Thanks.

If you're using migrations, then it will add this by default, unless you
tell it not to (meaning don't worry, it will be there). I'd probably look at
http://guides.rubyonrails.org/migrations.html as my first resource in that
situation, though you'll want to check your version of AR against whatever
they're using, they released AR 3 five days ago
http://rubygems.org/gems/activerecord those guides were last updated on July
15th. You're probably fine, but just be aware of that if examples don't seem
to be working.


If you're not using migrations, then you'll want to do something like this
http://github.com/JoshCheek/Play/blob/master/blackboardbot/create_database.rb

CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
subject Text,
body Text,
author Text,
message_board_id Integer
);


The second line defines the id, and will depend on your database, for
instance, I got the syntax for that one at
http://www.sqlite.org/lang_createtable.html
 
M

Markus Fischer

Hi,

How does the database server manage the incrementing of the key? What is
meant here by the database server is what manages the incrementing of
the key?

Is there an example that may make this point more clear. For example,
the following "database server" increments......

You need to consult your database documentation how to achieve this; it
varies from one product to another. E.g. in MySQL it's using
"auto_increment" on the primary key [1]

HTH

[1] http://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html
 
B

Brian Candler

Abder-Rahman Ali said:
How does the database server manage the incrementing of the key? What is
meant here by the database server is what manages the incrementing of
the key?

Mysql: primary key is declared as an AUTO_INCREMENT column.
http://dev.mysql.com/doc/refman/5.1/en/example-auto-increment.html

Sqlite: if there is an INTEGER PRIMARY KEY then it's the
automatically-assigned ROWID.
http://www.sqlite.org/autoinc.html

Oracle: uses a sequence, something like
INSERT INTO foo(id,bar) VALUES(foo_seq.nextval,?)

And so on - each of the database adapters has their own logic.

See for example
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

NATIVE_DATABASE_TYPES = {
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY
KEY".freeze,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top