更新时出现重复错误 mysql
Duplicate error on update mysql
我有一个包含多行的 table。有些字段 request_path
具有 .html.html
等字符,而其他字段具有不同的字符。我想删除所有 .html.html
并使它们成为单个 .html
。我正在尝试以下命令 -
UPDATE url_rewrite
SET request_path = CONCAT(LEFT(request_path, CHAR_LENGTH(request_path) -10), '.html')
WHERE request_path LIKE '%.html.html';
但它给出了以下错误 -
#1062 - Duplicate entry 'test/test.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID'
如果我删除了这个条目,它就会开始对其他一些值给出同样的错误。我无法超越这个错误。谁能帮忙解决这个问题?
table 的结构看起来像 -
通过更新 table 您试图创建 重复值 因为它违反了 table 的 键和约束。
您必须检查 url_rewrite table 中的 keys 并确保 列 您选择的键不能有重复值。
更新:
在 phpmyadmin 中进入 url_rewrite table 结构,在列下面有一个 'indexes' 按钮,点击它你会看到 [= 的索引名称38=] 列。
例如,如果索引名称是 request_path_index,运行 以下查询将删除该索引:
alter table url_rewrite drop index request_path_index
我认为你做不到。
当您执行 UPDATE 时,您更新的值将成为另一个相同值的副本。这违反了 table 的约束,因此违反了错误
我认为您在 request_path 列中设置了唯一键约束尝试使用此查询删除此约束,然后执行以下替换查询。
ALTER TABLE url_rewrite DROP INDEX URL_REWRITE_REQUEST_PATH_STORE_ID;
然后,
UPDATE url_rewrite
SET request_path = REPLACE(request_path, '.html.html', '.html')
这会将所有 '.html.html'
替换为单个 '.html'
如果您愿意,也可以在此处使用 where
条件。
然后再次使用以下查询重新分配约束:
ALTER TABLE url_rewrite add unique index URL_REWRITE_REQUEST_PATH_STORE_ID (request_path (255))
您不能 insert/update 将值复制到唯一的 属性 字段中。
要么更改字段 属性(删除唯一性 属性)
或
更新前更改 request_path 值
(初步检查新的请求路径是否已经存在,如果存在则重命名。)
根据您的要求
你可以试试:
UPDATE url_rewrite
SET request_path = REPLACE(request_path , '.html.html', '.html')
WHERE request_path LIKE ('%.html.html');
我有一个包含多行的 table。有些字段 request_path
具有 .html.html
等字符,而其他字段具有不同的字符。我想删除所有 .html.html
并使它们成为单个 .html
。我正在尝试以下命令 -
UPDATE url_rewrite
SET request_path = CONCAT(LEFT(request_path, CHAR_LENGTH(request_path) -10), '.html')
WHERE request_path LIKE '%.html.html';
但它给出了以下错误 -
#1062 - Duplicate entry 'test/test.html-1' for key 'URL_REWRITE_REQUEST_PATH_STORE_ID'
如果我删除了这个条目,它就会开始对其他一些值给出同样的错误。我无法超越这个错误。谁能帮忙解决这个问题?
table 的结构看起来像 -
通过更新 table 您试图创建 重复值 因为它违反了 table 的 键和约束。
您必须检查 url_rewrite table 中的 keys 并确保 列 您选择的键不能有重复值。
更新:
在 phpmyadmin 中进入 url_rewrite table 结构,在列下面有一个 'indexes' 按钮,点击它你会看到 [= 的索引名称38=] 列。
例如,如果索引名称是 request_path_index,运行 以下查询将删除该索引:
alter table url_rewrite drop index request_path_index
我认为你做不到。
当您执行 UPDATE 时,您更新的值将成为另一个相同值的副本。这违反了 table 的约束,因此违反了错误
我认为您在 request_path 列中设置了唯一键约束尝试使用此查询删除此约束,然后执行以下替换查询。
ALTER TABLE url_rewrite DROP INDEX URL_REWRITE_REQUEST_PATH_STORE_ID;
然后,
UPDATE url_rewrite
SET request_path = REPLACE(request_path, '.html.html', '.html')
这会将所有 '.html.html'
替换为单个 '.html'
如果您愿意,也可以在此处使用 where
条件。
然后再次使用以下查询重新分配约束:
ALTER TABLE url_rewrite add unique index URL_REWRITE_REQUEST_PATH_STORE_ID (request_path (255))
您不能 insert/update 将值复制到唯一的 属性 字段中。
要么更改字段 属性(删除唯一性 属性)
或
更新前更改 request_path 值 (初步检查新的请求路径是否已经存在,如果存在则重命名。)
根据您的要求
你可以试试:
UPDATE url_rewrite
SET request_path = REPLACE(request_path , '.html.html', '.html')
WHERE request_path LIKE ('%.html.html');