【问题标题】:django.db.utils.IntegrityError: null value in column "old_column" violates not-null constraintdjango.db.utils.IntegrityError:“old_column”列中的空值违反非空约束
【发布时间】:2021-07-19 15:22:43
【问题描述】:

我在测试中遇到了一个非常奇怪的错误:

django.db.utils.IntegrityError: null value in column "old_column" violates not-null constraint

E               DETAIL:  Failing row contains (68, , f, , null, f).

但是“old_column”不再存在于我的数据库中。

它是几个小时前的,但此列已重命名为不同的名称。

如果我使用psql mydbmanage.py dbshell 检查数据库,然后使用\d appname_tablename 检查数据库,那么我清楚地看到“old_column”不再存在。

我不知道为什么 DB (PostgreSQL) 认为该列仍然存在。

【问题讨论】:

    标签: django pytest pytest-django


    【解决方案1】:

    如果您使用“reuse-db”选项,那么 pytest 会保留测试数据库。

    如果你使用 postgres,那么你可以像这样列出可用的数据库:

    psql -l
                                    List of databases
         Name      |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
    ---------------+----------+----------+---------+---------+-----------------------
     postgres      | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
     template0     | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
                   |          |          |         |         | postgres=CTc/postgres
     template1     | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
                   |          |          |         |         | postgres=CTc/postgres
     test_mydb     | vagrant  | UTF8     | C.UTF-8 | C.UTF-8 | 
     mydb          | vagrant  | UTF8     | C.UTF-8 | C.UTF-8 | 
    (5 rows)
    

    如果您连接到“mydb”(通过psqlmanage.py dbshell),那么这是一个与您的测试不同的数据库。

    使用--create-db 强制重新创建数据库。

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 1970-01-01
      • 2021-12-06
      • 2016-04-04
      • 2021-11-02
      • 1970-01-01
      • 2016-02-14
      • 2017-02-18
      • 2020-08-12
      相关资源
      最近更新 更多