最近对一个django项目进行修改,其中在models.py添加了新的字段,需要同步到数据库。
尝试使用
python manage.py syncdb 和
python manage.py makemigrations,python manage.py migrate
都提示修改成功,到数据库中查看,新字段并没有同步进来。
解决办法如下:
python manage.py shell
进入后
from django.db import connection
新建一个cursor,用于连接
cursor=conneciton.cursor()
下一步就可以进行对数据库的操作了,把在model中定义的量加入表中:
cursor.execute('ALTER TABLE table_name add port Integer default 0')
sql 语法:ALTER TABLE table_name add col_name col_type default ..