【问题标题】:SQL Sqlalchemy Auto-increment of Primary key didnot workSQL Sqlalchemy 主键的自动增量不起作用
【发布时间】:2021-10-29 15:08:34
【问题描述】:

当我跑desc features

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(128) | YES  | UNI | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

我有如下表,

+-----------+----------------------------------------------------------+
| id        | name                                                     |
+-----------+----------------------------------------------------------+
| 105314659 | latitude                                                 |
| 105314658 | final                                                    |
+-----------+----------------------------------------------------------+

我跑了INSERT INTO features (name) VALUES ('test'),它没有给出自增主键,比如

+-----------+----------------------------------------------------------+
| id        | name                                                     |
+-----------+----------------------------------------------------------+
| 109728684 | test                                                     |
| 105314659 | latitude                                                 |
| 105314658 | final                                                    |
+-----------+----------------------------------------------------------+
+-----------+----------------------------------------------------------+
| id        | name                                                     |
+-----------+----------------------------------------------------------+
| 109728690 | test5                                                    |
| 109728688 | test4                                                    |
| 109728687 | test3                                                    |
| 109728686 | test2                                                    |
| 109728684 | test                                                     |
| 105314659 | latitude                                           |
| 105314658 | final                                                    |
+-----------+----------------------------------------------------------+

有人有什么想法吗?谢谢!

*** 这个表我恒定deleteinsert BTW。但是当我运行单次插入时,它没有自动增量id ***

【问题讨论】:

  • 你看SHOW CREATE TABLE features;的结果了吗?这应该指示当前的 auto_increment 值。
  • 是的,我做到了。谢谢你指出。原来我有ERROR 1062 (23000): Duplicate entry 'test4' for key 'features.ix_features' 错误,它消耗了下一个AUTO_INCREMENT id。谢谢

标签: sql sqlalchemy sql-insert


【解决方案1】:

这个问题来自

INSERT INTO nlp_request_feature_types (name) VALUES ('test4');,

这会导致exc.IntegrityError,比如 ERROR 1062 (23000): Duplicate entry 'test4' for key 'features.ix_features',但 id = 109728689 将被分配。

所以下次运行INSERT INTO nlp_request_feature_types (name) VALUES ('test5');时,会成功存入表中,主键id = 109728690

因此,它会给我们一种auto-incremental主键不起作用的错觉。但是,它确实有效,只是因为我们的错误 ERROR 1062 消耗了一个 id。

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2017-03-09
    • 2023-03-13
    • 2016-08-08
    • 1970-01-01
    • 2015-08-06
    • 2015-02-14
    • 2014-04-21
    • 2010-10-11
    相关资源
    最近更新 更多