完整错误:
Warning: Unsafe statement written to the binary log using statement format
since BINLOG_FORMAT = STATEMENT. Statements writing to a table with an
auto-increment column after selecting from another table are unsafe because
the order in which rows are retrieved determines what (if any) rows will be
written. This order cannot be predicted and may differ on master and the
slave.
我似乎只在尝试从 Django 运行原始 SQL 时收到此错误。如果我从 MySQL CLI 客户端运行 SQL,我不会收到相同的错误。这是SQL:
UPDATE picture p
JOIN (
SELECT @inc := @inc + 1 AS new_weight, id
FROM (SELECT @inc := 0) temp, (
SELECT id FROM picture
WHERE album_id = 5
ORDER BY taken_date ASC
) AS pw
) AS pw
ON p.id = pw.id
SET p.weight = pw.new_weight;
这样做的目的是对记录进行排序,并将序号应用于权重,以便在数据库中保留此排序。
我已尝试在客户端中运行此命令以查看是否可以复制该问题,但它仍然可以成功运行:
mysql> SET GLOBAL binlog_format = 'STATEMENT';
此外,我解决或重写 SQL 以处理此约束也很重要,因为最终应用程序几乎肯定会跨主从数据库设置运行。
*编辑:阅读更多内容后binlog_format
,似乎 ROW 或 MIXED 完全可以接受,但是我主要担心的是无法在 MySQL CLI 中复制此问题以测试 MIXED/ROW 是否可以解决此问题?