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

这样就可以创建表了。


点个赞呗~

相关推荐
better_liang11 分钟前
每日Java面试场景题知识点之-MySQL索引
java·数据库·mysql·性能优化·索引
AgCl2312 分钟前
MYSQL-4-DQL数据查询语言-3/14-15
数据库·mysql
别抢我的锅包肉18 分钟前
【MySQL】第五节 - 事务实战详解:从基础到并发控制(附 Navicat 可运行实验脚本)
数据库·mysql
AgCl231 小时前
MYSQL-5-DCL数据查询语言-3/16
数据库·mysql
IvorySQL1 小时前
PostgreSQL 技术日报 (4月7日)|内核开发新动态,多项功能优化落地
数据库·postgresql·开源
IvorySQL1 小时前
PostgreSQL 技术日报 (4月6日)|内核补丁与性能优化速递
数据库·postgresql·开源
IvorySQL2 小时前
PostgreSQL 技术日报 (4月5日)|六大核心补丁进展,生产环境必看
数据库·postgresql·开源
想唱rap2 小时前
线程的同步与互斥
linux·运维·服务器·数据库·mysql
IvorySQL2 小时前
走进高校,走近开源|IvorySQL 社区即将亮相应急管理大学开源学术报告会
数据库·postgresql·开源
splage2 小时前
Oracle分页sql
数据库·sql·oracle