欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Django更新models数据库结构步骤

时间:2021-05-13 08:14:45|栏目:Python代码|点击:

有时候在我们使用Django设计了models中的数据库结构,并且已经同步了数据库之后,我们突然想在数据表中更新或者增加新的字段,也就是需要修改数据库的结构,会出现以下的问题:

C:\Users\Administrator\Desktop\Web开发\Django_Demo\jkxy>python manage.py makemigrations
You are trying to add a non-nullable field 'grade' to student without a default; we can't do that (the dat
abase 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 let me add a default in models.py
Select an option:

就是在我们在models中修改了表的字段后,进行python manage.py makemigrations同步数据库时会出现上面报错,会导致数据库结构更新失败

解决方法如下:

第一种方式:先删除再重构

1、删除数据库对应的数据表

注意:在这里可以不用暴力删除数据表,可以利用django的migrations进行,操作如下:

1.1、首先将自己需要重构的数据表类的models注释掉,然后输入命令python manage.py makemigrations,这个时候migration会自动记录删除数据表的操作

1.2、然后在输入命令python manage.py migrate,Django会自动将本地对应的数据库进行删除

2、删除应用当中的migrations文件

3、删除应用当中的pychace文件

4、删除db_sqllite文件(若配置数据库为mysql时,可以删除db_sqllite)

5、建立一个空数据库,命令为python manage.py makemigrations --empty 应用名称

C:\Users\Administrator\Desktop\Web开发\Django_Demo\jkxy>python manage.py makemigrations --empty bbs
Migrations for 'bbs':
bbs\migrations\0001_initial.py

6、同步数据库:

python manage.py makemigrations
python manage.py migrate

 
C:\Users\Administrator\Desktop\Web开发\Django_Demo\jkxy>python manage.py makemigrations --empty bbs
Migrations for 'bbs':
 bbs\migrations\0001_initial.py
 
C:\Users\Administrator\Desktop\Web开发\Django_Demo\jkxy>python manage.py makemigrations
Migrations for 'bbs':
 bbs\migrations\0002_student_testmyfield.py
 - Create model Student
 - Create model Testmyfield
 
C:\Users\Administrator\Desktop\Web开发\Django_Demo\jkxy>python manage.py migrate
Operations to perform:
 Apply all migrations: admin, auth, bbs, contenttypes, sessions
Running migrations:
 Applying contenttypes.0001_initial... OK
 Applying auth.0001_initial... OK
 Applying admin.0001_initial... OK
 Applying admin.0002_logentry_remove_auto_add... OK
 Applying contenttypes.0002_remove_content_type_name... OK
 Applying auth.0002_alter_permission_name_max_length... OK
 Applying auth.0003_alter_user_email_max_length... OK
 Applying auth.0004_alter_user_username_opts... OK
 Applying auth.0005_alter_user_last_login_null... OK
 Applying auth.0006_require_contenttypes_0002... OK
 Applying auth.0007_alter_validators_add_error_messages... OK
 Applying auth.0008_alter_user_username_max_length... OK
 Applying bbs.0001_initial... OK
 Applying bbs.0002_student_testmyfield... OK
 Applying sessions.0001_initial... OK

第二种方式:直接在原结构上更新结构

#出版社
class Publisher(models.Model):
 '''出版社数据表'''
 id=models.AutoField(primary_key=True) #自增ID主键
 name=models.CharField(max_length=50,verbose_name='出版社名称',null=False,unique=True)
 
 def __str__(self):
  return '{},{}'.format(self.id,self.name)

接下来我们需要新增一个字段addr地址

#出版社
class Publisher(models.Model):
 '''出版社数据表'''
 id=models.AutoField(primary_key=True) #自增ID主键
 name=models.CharField(max_length=50,verbose_name='出版社名称',null=False,unique=True)
 addr=models.CharField(max_length=128,verbose_name='出版社地址')
 
 def __str__(self):
  return '{},{}'.format(self.id,self.name,self.addr)

由于我们的数据库的Publisher表本身已经有数据了,所以我们在进行python manage.py makemigrations会出现以下错误:

上图的意思是说addr没有默认值,无法更新到数据库中,然后给出了两个选项:

第一是让你马上给定一个默认值,然后一次性的把所有addr都写成这个值

第二是先退出,你自己在models里面去配置默认值

我选择先退出,在models中加上默认值,在进行makemigrations,这次就会更新成功了!

#出版社
class Publisher(models.Model):
 '''出版社数据表'''
 id=models.AutoField(primary_key=True) #自增ID主键
 name=models.CharField(max_length=50,verbose_name='出版社名称',null=False,unique=True)
 addr=models.CharField(max_length=128,verbose_name='出版社地址',default='成都市动物园')
 
 def __str__(self):
  return '{},{}'.format(self.id,self.name,self.addr)

让我们来检查数据库中的数据表:

可以看到数据结构发生了变化!

上一篇:python多线程高级锁condition简单用法示例

栏    目:Python代码

下一篇:Python cookbook(数据结构与算法)同时对数据做转换和换算处理操作示例

本文标题:Django更新models数据库结构步骤

本文地址:http://www.codeinn.net/misctech/120708.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有