ActiveRecord and incrementing the key

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.

Abder-Rahman A. wrote:

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,

Thanks @Brian for the clarification.

Hi,

On 04.09.2010 17:17, Abder-Rahman A. wrote:

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

On Sat, Sep 4, 2010 at 10:17 AM, Abder-Rahman A. <
[email protected]> wrote:

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.

Posted via http://www.ruby-forum.com/.

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

Thanks @Josh.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs