# Auto\_increment question?

**URL:** <https://www.ruby-forum.com/t/auto-increment-question/164770>\
**Category:** Rails\
**Created:** [April 5, 2009, 6:39pm UTC](https://www.ruby-forum.com/t/auto-increment-question/164770 "2009-04-05T18:39:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingduggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@kingduggan](https://www.ruby-forum.com/u/kingduggan)\
**Post date:** [April 5, 2009, 6:39pm UTC](https://www.ruby-forum.com/t/auto-increment-question/164770/1 "2009-04-05T18:39:46Z")

</div>

Hello All,

So I am new to rails and the first project I am creating is a  
ticketing system to log customer support. All I am trying to do is  
have the primary id of each new ticket auto increment like ticket id  
1001, 1002, 1003, ect. and display the ticket id in the show tickets.

I am confused because I’m not sure if this would be created doing a  
migration or in the model tickets after the migration has been done??

Any help would be great.

Thanks,

Duggan

---

<div class="post-metadata">

**Author:** ![kingduggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@kingduggan](https://www.ruby-forum.com/u/kingduggan)\
**Post date:** [April 5, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/auto-increment-question/164770/2 "2009-04-05T20:16:41Z")

</div>

kingduggan wrote:

> Hello All,
> 
> So I am new to rails and the first project I am creating is a  
> ticketing system to log customer support. All I am trying to do is  
> have the primary id of each new ticket auto increment like ticket id  
> 1001, 1002, 1003, ect. and display the ticket id in the show tickets.
> 
> I am confused because I’m not sure if this would be created doing a  
> migration or in the model tickets after the migration has been done??
> 
> Any help would be great.

Unless I’m not understanding the question, you already have this.

With ActiveRecord, (by default) every record has an id that will be  
unique. You don’t have to do anything to get it. While you can override  
it, every table has a field named ‘id’. This will be incremented and  
maintained by the system. In MySQL it will be an ‘auto\_increment’ field  
and defined as the primary key.

—Michael

---

<div class="post-metadata">

**Author:** ![kingduggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@kingduggan](https://www.ruby-forum.com/u/kingduggan)\
**Post date:** [April 6, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/auto-increment-question/164770/3 "2009-04-06T17:18:05Z")

</div>

Thank you very much. You are right about the id and that is sufficient  
for  
me to use as ticket id. Now just for personal know how, How can I change  
the  
id from numbering 1,2,3 to 1001, 1002, 1003?

On Sun, Apr 5, 2009 at 1:16 PM, Michael S. \<

---

<div class="post-metadata">

**Author:** ![kingduggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@kingduggan](https://www.ruby-forum.com/u/kingduggan)\
**Post date:** [April 6, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/auto-increment-question/164770/4 "2009-04-06T18:04:03Z")

</div>

Duggan Roberts wrote:

> Thank you very much. You are right about the id and that is sufficient  
> for  
> me to use as ticket id. Now just for personal know how, How can I change  
> the  
> id from numbering 1,2,3 to 1001, 1002, 1003?
> 
> On Sun, Apr 5, 2009 at 1:16 PM, Michael S. \<

Either as a migration or as raw SQL, enter the following statement:

Alter table AUTO\_INCREMENT = 1000;

---

<div class="post-metadata">

**Author:** ![kingduggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/3d9bf3/32.png) [@kingduggan](https://www.ruby-forum.com/u/kingduggan)\
**Post date:** [April 6, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/auto-increment-question/164770/5 "2009-04-06T18:29:09Z")

</div>

Is it approved of to use the id as a meaningful number? I have always  
assumed that you should not do this as, for example, once you have added  
number 1000 and deleted it (possibly by mistake) you cannot ever use  
this  
number again (without hacking). If you need a meaningful number then I  
would suggest adding a ticket\_number field and controlling this by hand.

2009/4/6 Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
