[英]MySQL INSERT not working as expected
This query is running an upsert which is being called from PHP, I wasn't getting the updates except for one table, which was really odd. 这个查询正在运行一个upsert,该更新是从PHP调用的,除了一个表之外,我没有得到更新,这确实很奇怪。 I tried the query and noticed this was seeming to be the case.
我尝试查询,发现情况似乎如此。 If I run it for the following value, the row does not update.
如果我为以下值运行它,则该行不会更新。
Row 行
operation_attendee_id operation_id member_id status
1 5 1 1
Query: 查询:
INSERT INTO
`operation_attendee` (
`operation_id`,
`member_id`,
`status`)
VALUES
(5,
1,
3)
ON DUPLICATE KEY UPDATE
`status` = 3
This will not update the table, but there is a unique index on operation_id, member_id 这不会更新表,但是在operation_id,member_id上有唯一索引
If I run for this row 如果我跑到这一行
operation_attendee_id operation_id member_id status
0 7 1 1
Same exact query, different numbers 完全相同的查询,不同的数字
INSERT INTO
`operation_attendee` (
`operation_id`,
`member_id`,
`status`)
VALUES
(7,
1,
3)
ON DUPLICATE KEY UPDATE
`status` = 3
This one updates fine. 这一更新很好。 I'm really perplexed as to why this seems to be the only value set that is updating.
对于为什么这似乎是唯一要更新的值集,我确实感到困惑。
EDIT: Adding table DDL and all indexes: 编辑:添加表DDL和所有索引:
CREATE TABLE `operation_attendee` (
`operation_attendee_id` int(11) NOT NULL,
`operation_id` int(11) NOT NULL,
`member_id` int(11) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`operation_attendee_id`),
UNIQUE KEY `uk_operation_member` (`operation_id`,`member_id`),
KEY `fk_operation_attendee_operation1_idx` (`operation_id`),
KEY `fk_operation_attendee_member1_idx` (`member_id`),
KEY `fk_operation_attendee_operation_attendee_status1_idx` (`status`),
CONSTRAINT `fk_operation_attendee_member1` FOREIGN KEY (`member_id`) REFERENCES `member` (`member_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_operation_attendee_operation1` FOREIGN KEY (`operation_id`) REFERENCES `operation` (`operation_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_operation_attendee_operation_attendee_status1` FOREIGN KEY (`status`) REFERENCES `operation_attendee_status` (`operation_attendee_status_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8
I also tried to run the query with foreign key check off and it still doesn't update. 我还尝试使用外键检查运行查询,但它仍然没有更新。
and 和
It seems it was an issue with the surrogate key. 似乎这是代理密钥的问题。
I removed it (it was not needed for that table, someone else built it a long while back), and then replace the primary key with the unique key I was using prior (removed the unique key as well since it was redundant) the query now seems to be working fine. 我删除了它(该表不需要它,其他人很久以前就建立了它),然后用我之前使用的唯一键替换主键(也删除了唯一键,因为它很多余)查询现在似乎工作正常。
I'm not sure why it was causing issues in terms of an underlying reason, but it works now. 我不确定为什么会从根本原因上引起问题,但现在可以使用了。 I would think the unique key I put on the table would have matched the conditions for the ON DUPLICATE KEY UPDATE requirement.
我认为我放在桌上的唯一键会符合ON DUPLICATE KEY UPDATE要求的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.