# ActiveRecord and incrementing the key

**URL:** <https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413>\
**Category:** Ruby\
**Created:** [September 4, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413 "2010-09-04T17:17:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 4, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413/1 "2010-09-04T17:17:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 4, 2010, 5:48pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413/2 "2010-09-04T17:48:13Z")

</div>

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](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](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,

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 4, 2010, 5:51pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413/3 "2010-09-04T17:51:48Z")

</div>

Thanks @Brian for the clarification.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413/4 "2010-09-06T16:39:27Z")

</div>

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](http://dev.mysql.com/doc/refman/5.6/en/example-auto-increment.html)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 6, 2010, 4:39pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413/5 "2010-09-06T16:39:29Z")

</div>

On Sat, Sep 4, 2010 at 10:17 AM, Abder-Rahman A. \<  
removed\_email\_address@domain.invalid\> 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/](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  
[Active Record Migrations — Ruby on Rails Guides](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  
[activerecord | RubyGems.org | your community gem host](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

> <https://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](http://www.sqlite.org/lang_createtable.html)

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [September 6, 2010, 4:58pm UTC](https://www.ruby-forum.com/t/activerecord-and-incrementing-the-key/194413/6 "2010-09-06T16:58:37Z")

</div>

Thanks @Josh.
