简体   繁体   English

MySql 在 INSERT INTO 时生成相同的 UUID... SELECT

[英]MySql generating the same UUID when INSERT INTO… SELECT

I have this bug happening only on the production server (of course).我有这个错误只发生在生产服务器上(当然)。 I'm not able to replicate this on any machine.我无法在任何机器上复制它。

Let's say I have to perform a query made like this假设我必须执行这样的查询

INSERT INTO my_table(UUID, Name, Surname) SELECT UUID(), Name, Surname FROM other_table

I expect to have a different UUID for any new generated row, that is exactly what happens in any environment.我希望任何新生成的行都有不同的 UUID,这正是在任何环境中发生的情况。 Except production.除了生产。

There, if I run that query, MySQL generates all identical UUID.在那里,如果我运行该查询,MySQL 会生成所有相同的 UUID。 Looks like that, instead of generating a new one for every row, it runs the function only once and then replicates the value.看起来像这样,它不是为每一行生成一个新的,而是只运行 function 一次,然后复制该值。

I'm pretty sure it's due to some kind of configuration, but I can't understand which one.我很确定这是由于某种配置,但我不明白是哪一个。

Also, it might not be strictly MySQL related: if I run the query from my PC but on the production server using SQLyog, the query works fine and generates all different UUIDs.此外,它可能不严格与 MySQL 相关:如果我从我的 PC 上运行查询,但在使用 SQLyog 的生产服务器上运行,查询工作正常并生成所有不同的 UUID。

Could it be PHP fault?会不会是 PHP 故障? I'm currently using mysqli driver.我目前正在使用 mysqli 驱动程序。

Just to make it clear, if I run this query from my PC为了清楚起见,如果我从我的电脑上运行这个查询

SELECT UUID()
FROM (SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id UNION ALL
      SELECT 1 AS id
     ) i

the result is结果是

bb79716f-f0ba-11e9-a154-f23c91fbfa4a
bb79717a-f0ba-11e9-a154-f23c91fbfa4a
bb79717c-f0ba-11e9-a154-f23c91fbfa4a
bb79717e-f0ba-11e9-a154-f23c91fbfa4a
bb79717f-f0ba-11e9-a154-f23c91fbfa4a
bb797181-f0ba-11e9-a154-f23c91fbfa4a
bb797183-f0ba-11e9-a154-f23c91fbfa4a
bb797185-f0ba-11e9-a154-f23c91fbfa4a
bb797187-f0ba-11e9-a154-f23c91fbfa4a
bb797189-f0ba-11e9-a154-f23c91fbfa4a

But if I run the same query using PHP on the server, the result is但是如果我在服务器上使用 PHP 运行相同的查询,结果是

0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79
0aef9b78-f0bc-11e9-bfe5-7a7919257c79

Not really the perfect answer to the question, but if anybody has the same problem it can be solved with a workaround.并不是这个问题的完美答案,但如果有人遇到同样的问题,可以通过解决方法来解决。 Just create a "new_uuid" function in MySQL, in use it instead of the built-in one.只需在 MySQL 中创建一个“new_uuid”function,使用它而不是内置的。 This way这边走

CREATE FUNCTION new_uuid() RETURNS VARCHAR(36) CHARSET utf8 BEGIN   RETURN UUID();  END;

Then you can just use it instead of UUID()然后你可以使用它而不是 UUID()

SELECT new_uuid()

And it will correctly return different UUIDs in case you have multiple rows.如果您有多行,它将正确返回不同的 UUID。

This is the only approach I got working to solve my issue.这是我解决问题的唯一方法。 No idea why:)不知道为什么:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM