# Modifing db

**URL:** <https://www.ruby-forum.com/t/modifing-db/168517>\
**Category:** Rails\
**Created:** [May 21, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/modifing-db/168517 "2009-05-21T20:20:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 21, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/modifing-db/168517/1 "2009-05-21T20:20:03Z")

</div>

If I am going to build a sql database for use in rails (rather than  
building it in rails), is there anything I need to know beyond using an  
id column in every table? Does the id need to be the “primary key”?

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 21, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/modifing-db/168517/2 "2009-05-21T20:25:11Z")

</div>

Mk 27 wrote:

> If I am going to build a sql database for use in rails (rather than  
> building it in rails), is there anything I need to know beyond using an  
> id column in every table? Does the id need to be the “primary key”?

By convention, rails would look for an id as a primary key for all  
tables. Also, if you use rails migrations to generate the tables, you  
can say -  
create\_table(:table\_name, :id =\> false) if you don’t want id.  
This might be required in case your table is from a legacy system.

Thanks

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 21, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/modifing-db/168517/3 "2009-05-21T20:22:41Z")

</div>

Yes, the id needs to be the primary key.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 21, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/modifing-db/168517/4 "2009-05-21T20:25:37Z")

</div>

The id should be an auto-incrementing primary key.  
Also, column names should be lower cased using an underscore to  
separate words, i.e. first\_name  
You should also consider adding “created\_at” and “updated\_at” datetime  
fields to handle timestamps.

Table names should be pluralized.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 21, 2009, 9:03pm UTC](https://www.ruby-forum.com/t/modifing-db/168517/5 "2009-05-21T21:03:09Z")

</div>

E. Litwin wrote:

> The id should be an auto-incrementing primary key.  
> Also, column names should be lower cased using an underscore to  
> separate words, i.e. first\_name  
> You should also consider adding “created\_at” and “updated\_at” datetime  
> fields to handle timestamps.
> 
> Table names should be pluralized.

Thanks. I just realized I confused this post with one I forgot writing  
yesterday. This looks like the info I need.

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 22, 2009, 6:16am UTC](https://www.ruby-forum.com/t/modifing-db/168517/6 "2009-05-22T06:16:40Z")

</div>

> Does the id need to be the “primary key”?

You can set your own primary key like

def self.up  
create\_table :table\_name, :primary\_key =\> :primary\_key\_field\_name, do  
|t|  
t.integer :primary\_key\_field\_name  
---------  
end

Sijo

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 21, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/modifing-db/168517/7 "2009-05-21T21:48:08Z")

</div>

Mk 27 wrote:

> If I am going to build a sql database for use in rails (rather than  
> building it in rails),  
> […]

Why would you do that? I understand the use case of a preexisting  
“legacy” DB, but if you’re building a DB from scratch for use with a  
Rails app, then it’s just more efficient to use the DB tools that Rails  
provides.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
