0

I have an almost finished Rails 3.2.11 app that I've been developing with SQLite3. It works perfectly with SQLite3. Now I want to switch to MySQL for the rest of development but something's going wrong.

I have a unique index on the 'guid' column of my 'feed_entries' table. The app first checks to see whether a record with the same 'guid' exists and only creates a new one if there isn't one with same guid already. For some reason, the guid is being changed to a specific number and so only one record gets saved.

Here is the error:

ActiveRecord::RecordNotUnique: Mysql::Error: Duplicate entry '2147483647' for key 'index_feed_entries_on_guid' 

Here are the records that are attempting to get saved:

FeedEntry Exists (0.3ms)  SELECT 1 AS one FROM `feed_entries` WHERE `feed_entries`.`guid` = 3577996865 LIMIT 1
SQL (0.1ms)  BEGIN
FeedEntry Exists (0.3ms)  SELECT 1 AS one FROM `feed_entries` WHERE `feed_entries`.`guid` = BINARY 3577996865 LIMIT 1
SQL (1.0ms)  INSERT INTO `feed_entries` (`guid`) VALUES (?)  [["guid", 3577996865]]
(1.0ms)  COMMIT
FeedEntry Exists (0.2ms)  SELECT 1 AS one FROM `feed_entries` WHERE `feed_entries`.`guid` = 3643574649 LIMIT 1
SQL (0.1ms)  BEGIN
FeedEntry Exists (0.3ms)  SELECT 1 AS one FROM `feed_entries` WHERE `feed_entries`.`guid` = BINARY 3643574649 LIMIT 1
SQL (1.6ms)  INSERT INTO `feed_entries` (`guid`) VALUES (?)  [["guid", 3643574649]]
(0.8ms)  ROLLBACK

I stripped the unimportant parts. The table has more than 20 columns, but the guid is the only meaningful one at record creation so I deleted the rest of the info for brevity and ease of reading.

As you can see, it shows as if it's saving them with the proper guid, but then the error shows that it's trying to save with guid 2147483647. After the error, the table has only one record and although it shows that the record was saved with guid 3577996865, when it fetches it from the db, it has the guid shown in the error (2147483647). I don't understand why it's converting the guids to that number.

Maybe it has something to do with the second Exists statement that says BINARY before the guid? When I switch branches into one that still uses the SQLite db, there's only one Exists query per record it tries to save.

Thanks for reading, any help is greatly appreciated.

1 Answer 1

1

I figured it out! The guid is larger than the 4 byte 'int' field type. The upper limit is around 2.1 billion (2147483647). My solution was just to chop the first digit off because I don't need it. If I did, I could change the data type to 'bigint'.

Here's an explanation: http://www.idytise.com/blog/returning-2147483647-in-your-mysql-database-table/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.