Symptom:
General system exception happened. SQLSTATE[22003]: Numeric value out of range: 167 Out of range value for column 'value_id' at row 1, query was: INSERT INTO
catalog_product_entity_varchar
(entity_id
,attribute_id
,store_id
,value
) VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (
This script copies out the data into a temp table, fixes the auto-increment, then pipes the data back in cleanly.
Run each line independently and note the instructions for continuing.
Take notes on the counts, etc.
I highly recommend taking a database dump before beginning, just in case.
Fair warning, there are a lot of steps here, but most of them are double-checking. Since you're most likely going to be running this in production, it's best to be safe rather than sorry.
- command line to the Magento instance
- SQL edit access to the database
- connection of choice for downloading backups/exports
php bin/magento maintenance:enable
select
max(value_id)
from
catalog_product_entity_varchar;
- Note the output: current max id =
select
`AUTO_INCREMENT`
from
INFORMATION_SCHEMA.TABLES
where
TABLE_NAME = 'catalog_product_entity_varchar';
- Note the output: current autoincrement =
4. (optional) Dump the original table to csv and/or sql using whatever method you choose, just in case.
drop table if exists catalog_product_entity_varchar_copy;
create table catalog_product_entity_varchar_copy like catalog_product_entity_varchar;
This will probably take a few minutes.
insert into catalog_product_entity_varchar_copy
select
*
from
catalog_product_entity_varchar;
select
'catalog_product_entity_varchar' tablename,
count(value_id) as rowcount
from
catalog_product_entity_varchar
group by
tablename
union
select
'catalog_product_entity_varchar_copy' tablename,
count(value_id) as rowcount
from
catalog_product_entity_varchar_copy
group by
tablename;
This will also reset the auto increment.
truncate catalog_product_entity_varchar;
analyze table `catalog_product_entity_varchar`;
select
table_schema,TABLE_NAME,
`AUTO_INCREMENT`
from
INFORMATION_SCHEMA.TABLES
where
TABLE_NAME = 'catalog_product_entity_varchar';
This will probably take a few minutes.
insert into catalog_product_entity_varchar (attribute_id, store_id, `value`, entity_id)
select
attribute_id,
store_id,
`value`,
entity_id
from
catalog_product_entity_varchar_copy;
- If successful, no errors, note the record count and time it took:
- If no errors, skip down to step 10.
Example Error:
Cannot add or update a child row: a foreign key constraint fails (magento_replica_prod
.catalog_product_entity_varchar
, CONSTRAINT CAT_PRD_ENTT_VCHR_ATTR_ID_EAV_ATTR_ATTR_ID
FOREIGN KEY (attribute_id
) REFERENCES eav_attribute
(attribute_id
) ON DEL)
This is caused by data existing that doesn't comply with constraints that were defined on the table after the data was in there or perhaps a cascade was interrupted.
Either way, the following steps help you identify and double check that you can ignore these rows as they are not valid.
select distinct
attribute_id
from
catalog_product_entity_varchar_copy cpev_c
where
not exists (
select
*
from
eav_attribute eav
where
eav.attribute_id = cpev_c.attribute_id);
- Note the attribute_ids that no longer exist:
select
*
from
catalog_eav_attribute
where
attribute_id in (`your list here`);
select
*
from
eav_attribute
where
attribute_id in (`your list here`);
select
count(value_id)
from
catalog_product_entity_varchar_copy
where
attribute_id not in (`your list here`);
- Note the output: good records =
select
count(value_id)
from
catalog_product_entity_varchar_copy
where
attribute_id in (`your list here`);
- Note the output: dangling records =
This will probably take a few minutes.
insert into catalog_product_entity_varchar (attribute_id, store_id, `value`, entity_id)
select
attribute_id,
store_id,
`value`,
entity_id
from
catalog_product_entity_varchar_copy
where
attribute_id not in (`your list here`);
This will probably take a few minutes.
select
'catalog_product_entity_varchar_copy' tablename,
max(value_id) as max_id
from
catalog_product_entity_varchar_copy
group by
tablename
union
select
'catalog_product_entity_varchar' tablename,
max(value_id) as max_id
from
catalog_product_entity_varchar
group by
tablename;
analyze table `catalog_product_entity_varchar`;
select
table_schema,
auto_increment
from
information_schema.TABLES
where (TABLE_NAME = 'catalog_product_entity_varchar');
select
'catalog_product_entity_varchar_copy' tablename,
count(value_id) as rowcount
from
catalog_product_entity_varchar_copy
group by
tablename
union
select
'catalog_product_entity_varchar' tablename,
count(value_id) as rowcount
from
catalog_product_entity_varchar
group by
tablename;
bin/magento indexer:reset && bin/magento indexer:reindex && bin/magento c:f
bin/magento maintenance:disable
- Admin
- GraphQL
- REST
- Frontend
drop table if exists catalog_product_entity_varchar_copy;