mysql - 表中绝对没有数据时违反主键

标签 mysql google-app-engine google-cloud-sql

我有一个 Google App Engine 标准环境实例。我连接到 Google Cloud SQL 的应用程序出现以下错误 -

 Duplicate entry '1' for key 'PRIMARY' Query: INSERT INTO user_profiles
 (uid, name, profile_image_url, favourite_team_id, provider, admin)
 VALUES (?, ?, ?, ?, ?, ?) on duplicate key update name = ?,
 profile_image_url = ?, favourite_team_id = ?, provider = ?, admin = ?

真正奇怪的是 user_profiles 表是空的!

select count(*) from eplreflex.user_profiles 

count(*) 
0

看起来当我删除并重新创建模式时,mysql 实例仍然以某种方式在其中包含旧数据。究竟是怎么回事?如何删除包括旧数据在内的所有内容?

顺便说一句,这是表定义

CREATE TABLE `eplreflex`.`user_profiles` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `uid` VARCHAR(200) NOT NULL,
  `name` VARCHAR(200) NOT NULL,
  `profile_image_url` VARCHAR(400) NOT NULL,
  `favourite_team_id` INT NULL,
  `provider` VARCHAR(100) NULL,
  `email` VARCHAR(100) NULL,
  `purchased_coins` double not null default 0,
  `free_coins` double not null default 0,
  `coins_won` double not null default 0,
  `coins_lost` double not null default 0,
  `admin` bool not null default 0,
  `insert_ts` timestamp default current_timestamp,
  `update_ts` timestamp default current_timestamp on update current_timestamp,
  `ads_enabled` bool not null default 1,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`uid`),
  INDEX (`favourite_team_id` ASC),
  INDEX (`coins_won` ASC),
  CONSTRAINT
    FOREIGN KEY (`favourite_team_id`)
    REFERENCES `eplreflex`.`teams` (`id`)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT);

更新

所以我从表中做了一个计数 (*),它返回 0。

我尝试截断表格,它给了我

错误代码:1701。无法截断外键约束中引用的表 (eplreflexleaderboard_users,CONSTRAINT leaderboard_users_ibfk_1 FOREIGN KEY (user_id) 引用资料 eplreflex.user_profiles (id))

我从 leaderboard_users 中选择 count(*),它也返回零!!

Google Cloud Sql 出现严重问题。

最佳答案

这很奇怪。您没有为 PRIMARY 键字段 (id) 提供数据,该字段设置为自动增量,因此应该不可能在该字段上出现重复键错误。

我能看到的唯一可能性是该字段的 auto_increment 计数器不知何故不同步。如果您删除并重新创建表,这也不应该是可能的。

您可以尝试以下操作,以确保计数器设置为零:

ALTER TABLE user_profiles AUTO_INCREMENT=0;

此外,如果您想 100% 确定表是空的,并且自动递增计数器已重置,请发出以下语句:

TRUNCATE TABLE user_profiles;

这将删除所有数据并重置/清除索引。

探索的最后一种可能性是您的代码是否意外地尝试插入该行两次 - 但我不认为这是问题所在,因为这会给您带来 UID 归档的重复键错误,而不是ID 字段。

关于mysql - 表中绝对没有数据时违反主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38610323/

相关文章:

php - 用于移动验证的随机生成的 OTP 未更新到数据库

php - 来自 mysql/JSON 的 twitter bootstrap typeahead 源代码

java - 添加的外部 jar 在谷歌应用引擎中不起作用

mysql - 如何在 Google App Maker 的 Cloud SQL 中创建唯一行

mysql - 优化mysql索引

android - Appengine连接Android项目-添加账号

python - 在 Google App Engine 中使用 mapreduce 的简单反例

PostgreSQL autovacuum 导致性能显着下降

mysql - 使用 mysqli_connect() 将 Wordpress 连接到 Google App Engine 上的 Cloud SQL

php - "Connection Refused"或 "No Such File or Directory"