Rails 5.0.1 db: migrate - ActiveRecord :: RecordNotUnique: Mysql2 :: Error: Duplicate record 'for key' PRIMARY ': INSERT INTO `ar_internal_metadata`
I recently upgraded to Rails 5.0.1 from 4.2.x and while trying to migrate db: I ran into an issue with ar_internal_metadata which seems to be incompatible with MySQL5.
Reproducing (assuming Rails 5 environment with gems and database schema installed):
- Drop the ar_internal_metadata table from the development database (if any).
- Run "bundle exec rake db: migrate"
- Start it up again.
On the second run (regardless of whether migrations are pending or not), this error occurs:
ActiveRecord :: RecordNotUnique: Mysql2 :: Error: Duplicate record '' for key 'PRIMARY': INSERT INTO ar_internal_metadata
( created_at
, updated_at
) VALUES ('2017-04-30 20:49:17', '2017-04-30 20:49: 17 ')
The ar_internal_metadata schema tells me that MySQL5 does have the correct concatenation: the primary key field is configured as non-empty, but it defaults to null. And, since it's a varchar (255) type, it can't auto-increment. Apparently the ActiveRecord hardware does not provide a value for the "key".
mysql> desc ar_internal_metadata;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| key | varchar(255) | NO | PRI | NULL | |
| value | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
4 rows in set (0.02 sec)
Here is the content of ar_internal_metadata just before the second call to db: migrate:
mysql> select * from ar_internal_metadata;
+-----+-------+---------------------+---------------------+
| key | value | created_at | updated_at |
+-----+-------+---------------------+---------------------+
| | NULL | 2017-04-30 20:56:39 | 2017-04-30 20:56:39 |
+-----+-------+---------------------+---------------------+
1 row in set (0.00 sec)
Has anyone seen this issue? Any suggestions for workarounds or where to look? What value should ActiveRecord put in ar_internal_metadata for "key" (especially the second time db: migrate run)?
source to share
No one has answered this question yet
Check out similar questions: