Django 做migrations时出错,解决方案

在做migrations的时候,偶尔会出现出错。


在已有数据的表中新增字段时,会弹出下面的信息

运行这个命令时

python 复制代码
python manage.py makemigrations

Tracking file by folder pattern: migrations

It is impossible to add a non-nullable field 'example' to book without specifying a default. This is because the database needs something to populate existing rows.

Please select a fix:

  1. Provide a one-off default now (will be set on all existing rows with a null value for this column)
  2. Quit and manually define a default value in models.py.
    Select an option:

解决方案:

选择1

之后会弹出

Please enter the default value as valid Python.

The datetime and django.utils.timezone modules are available, so it is possible to provide e.g. timezone.now as a value.

Type 'exit' to exit this prompt

输入, 必须加双引号

python 复制代码
"1"

migrate 无法生成表

运行这个命令时

python 复制代码
python manage.py migrate

会出现这种结果

python 复制代码
Operations to perform:
  Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.

出现无法生成表的原因是,去到 django_migrations 表里查看,就能知道之前已经创建了表。所以得在该表用sql语句来删除对应的app数据。

最后运行下面两个命令

python 复制代码
python manage.py makemigrations

python manage.py migrate

这样就可以创建表了。


点个赞呗~

相关推荐
wyt5314294 小时前
Redis的安装教程(Windows+Linux)【超详细】
linux·数据库·redis
CeshirenTester4 小时前
从数据库到结构化用例:一套可落地的测试智能体架构
数据库·架构
2301_793804694 小时前
Python数据库操作:SQLAlchemy ORM指南
jvm·数据库·python
不想看见4046 小时前
Qt 项目中实现良好封装(模块化设计)的详细流程指南
数据库·系统架构
mygljx6 小时前
MySQL 数据库连接池爆满问题排查与解决
android·数据库·mysql
Jeremy爱编码6 小时前
软考数据库
数据库
Bdygsl7 小时前
MySQL(1)—— 基本概念和操作
数据库·mysql
zongzizz7 小时前
Oracle 11g 两节点rac在机房断电重启后PL/SQL和客户端连接数据库报错ORA-12541
数据库·oracle
qq_417695057 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
身如柳絮随风扬7 小时前
什么是左匹配规则?
数据库·sql·mysql