Register now and start sharing your code snippets.
-->

How to fix "Mysql::Error: Duplicate entry '2147483647' for key 3: INSERT INTO `xxx`"

Ruby posted 2 months ago by christian

2147483647 is the maximum for an integer column in MySQL, so this error probably means you’ve exceeded this limit somewhere in your code.

Rails automatically detects the best type for your columns, so be sure to specify the correct limit when creating the column with migrations:

   1  # from activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb
   2          case limit
   3          when 1; 'tinyint'
   4          when 2; 'smallint'
   5          when 3; 'mediumint'
   6          when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
   7          when 5..8; 'bigint'
   8          else raise(ActiveRecordError, "No integer type has byte size #{limit}")
   9          end

This Rails migration code would create a big integer column:

   1  t.integer :product_id, :null => false, :limit => 8

See the section on Numeric Types in the MySQL documentation for more information.

Tagged mysql, numeric, error, gotcha, migration

How to debug memory allocation problems in PHP with Xdebug

PHP posted 3 months ago by christian

Xdebug is a good tool for finding the root cause of memory allocation problems such as the one shown here:

   1  Fatal error: Allowed memory size of X bytes exhausted (tried to allocate X bytes)

First install Xdebug by following the Xdebug installation instructions.

Next surround the code you suspect is causing the problem with the following function calls:

   1  xdebug_start_trace('/tmp/mytrace');
   2  ...
   3  Bad bad PHP code
   4  ...
   5  xdebug_stop_trace();

Read this blog entry by splitbrain.org for more details

Tagged xdebug, php, memory, error, performance

Showing ActiveRecord error messages from jQuery Ajax actions and scripts

HTML (Rails) posted 6 months ago by christian

The HTML , in a layout file, for example application.html.erb:

   1  <div id="error-message" style="display:none">
   2  </div>

The JavaScript, rendered by for example create.js.erb:

   1  <% if !@category.valid? %>
   2  <%
   3    errors = <<ERR
   4    <p>Please fix the following errors:</p>
   5    <ul>
   6      #{@category.errors.collect{|err| "<li>" + err[0] + " " + err[1] + "</li>" } }
   7    </ul>
   8  ERR
   9  %>
  10  $('#error-message').html('<%= escape_javascript(errors) %>');
  11  $('#error-message').show();
  12  <% else %>
  13  $('#error-message').hide();
  14  <% end %>

Tagged ajax, error, jquery, rails