# Mysql and Auto\_Increment woes

**URL:** <https://www.ruby-forum.com/t/mysql-and-auto-increment-woes/92567>\
**Category:** Rails\
**Created:** [April 20, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/mysql-and-auto-increment-woes/92567 "2007-04-20T20:15:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [April 20, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/mysql-and-auto-increment-woes/92567/1 "2007-04-20T20:15:43Z")

</div>

I’ve had to deal with this from the beginning of my work with Rails and  
Mysql, but now it’s really putting me on full stop. I’ve started using  
Selenium and Selenium IDE to do system testing of my rails site (highly  
recommended, pick up selenium\_on\_rails plugin). Selenium IDE, when you  
click  
on a link, basically grabs the entire a tag to make sure it knows what  
link  
to click on. For this situation, it’s:

//a[@onclick=“new Ajax.Request(’  
[http://localhost:5000/portal\_tables/14/records/1](http://localhost:5000/portal_tables/14/records/1)’, {asynchronous:true,  
evalScripts:true, method:‘get’}); return false;”]

The problem is “portal\_tables/14”. The portal\_tables table is rebuilt  
before  
each test (fixtures), which means in MySQL that the id each portal\_table  
gets is now different. So when Selenium goes to click on this link I  
told it  
to look for, it can’t find it because the link is now:

//a[@onclick=“new Ajax.Request(’  
[http://localhost:5000/portal\_tables/26/records/1](http://localhost:5000/portal_tables/26/records/1)’, {asynchronous:true,  
evalScripts:true, method:‘get’}); return false;”]

So, how do I stop this from happening? Is there a mysql server  
configuration  
I can set that resets auto\_increment when a table is cleared? This is  
one of  
the more annoying “features” of MySQL vs Postgres (in which I’ve never  
seen  
this problem before).

I’m sure others have hit this problem, is it easily solvable?

Jason

---

<div class="post-metadata">

**Author:** ![jruyle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jruyle](https://www.ruby-forum.com/u/jruyle)\
**Post date:** [April 22, 2007, 6:31am UTC](https://www.ruby-forum.com/t/mysql-and-auto-increment-woes/92567/2 "2007-04-22T06:31:23Z")

</div>

On Apr 20, 2:14 pm, “Jason R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
…

> The problem is “portal\_tables/14”. The portal\_tables table is rebuilt before  
> each test (fixtures), which means in MySQL that the id each portal\_table  
> gets is now different. So when Selenium goes to click on this link I told it  
> to look for, it can’t find it because the link is now:  
> …  
> So, how do I stop this from happening? Is there a mysql server configuration  
> I can set that resets auto\_increment when a table is cleared? This is one of  
> the more annoying “features” of MySQL vs Postgres (in which I’ve never seen

You have IDs hard coded in your fixtures, right? You could try using  
a snippet like this:

```
ActiveRecord::Base.connection.tables.each do |table|
  max_id = ActiveRecord::Base.connection.select_value("SELECT

```

MAX(id) FROM #{table}").to\_i  
ActiveRecord::Base.connection.execute “ALTER TABLE #{table} SET  
auto\_increment = #{max\_id+1}”  
end

Dan M.  
[http://www.dcmanges.com/blog](http://www.dcmanges.com/blog)
