Django makemigations 不更新数据库
Django makemigations not updating database
当我对 models.py 进行更改时,我希望 django 在我 运行 python3 manage.py makemigrations 或 [=22= 时为我更新数据库结构] manage.py makemigrations 应用程序名称。它没有检测到任何变化。我以前遇到过一次这个问题,不得不删除数据库中的所有内容来更新它,这似乎有点过激。我做错了什么?
这是我刚刚添加的新行:
l3_interfaces = JSONField
(py38-venv) [xxxxxx@xxxxxxn]$ python3 manage.py makemigrations
No changes detected
models.py
的内容
from django.db import models #Generic for models
from django.contrib.auth.models import User #Required for dynamic user information
from django.forms import ModelForm #Custom form
from jsonfield import JSONField
#Unique order. Top of heirachy tree
class Order(models.Model):
order_name = models.CharField(max_length=100, unique=True)#, null=True, blank=True) #Unique name of order
created_by = models.ForeignKey(User, related_name='Project_created_by', on_delete=models.DO_NOTHING) #Person who created the order
created_at = models.DateTimeField(auto_now_add=True) #Date/Time order was created
def __str__(self):
return self.order_name
#For CE router definition. Required for all orders.
class Ce_Base(models.Model):
#Hardware models of router
ROUTER_MODELS = (
('CISCO2901', 'CISCO2901'),
('ISR4331', 'ISR4331'),
('CISCO1921', 'CISCO1921'),
('ISR4351', 'ISR4351'),
('ISR4451', 'ISR4451'),
('ISR4231', 'ISR4231'),
('ISR4431', 'ISR4431'),
('ISR4461', 'ISR4461'),
)
#Available regions in which the router can reside.
REGION = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
)
#Properties of CE Base
ce_hostname = models.CharField(max_length=15) #Hostname of router i.e. XXXXXXXXX
new = models.BooleanField() #Whether the router currently exists on the network
location = models.TextField(null=True, blank=True) #Address of the POP site/rack location. Required if new.
router_model = models.CharField(max_length=200, null=True, choices=ROUTER_MODELS) #Hardware model of the router i.e ISR4351. Required if new.
region = models.CharField(max_length=200, null=True, choices=REGION) #Region in which the router resides. Required if new.
#Foreign key to tie CE Base to an order
order_reference = models.ForeignKey(Order, null=True, on_delete=models.CASCADE) #Order reference
l3_interfaces = JSONField #Layer 3 interfaces
这是因为您没有在模型中添加新字段。你应该写的是:
l3_interfaces = JSONField()
此外,关于语义的快速说明:
makemigrations
不会更新您的数据库结构,它会生成迁移文件
migrate
确实通过应用生成的文件更新了您的数据库结构
当我对 models.py 进行更改时,我希望 django 在我 运行 python3 manage.py makemigrations 或 [=22= 时为我更新数据库结构] manage.py makemigrations 应用程序名称。它没有检测到任何变化。我以前遇到过一次这个问题,不得不删除数据库中的所有内容来更新它,这似乎有点过激。我做错了什么?
这是我刚刚添加的新行:
l3_interfaces = JSONField
(py38-venv) [xxxxxx@xxxxxxn]$ python3 manage.py makemigrations
No changes detected
models.py
的内容from django.db import models #Generic for models
from django.contrib.auth.models import User #Required for dynamic user information
from django.forms import ModelForm #Custom form
from jsonfield import JSONField
#Unique order. Top of heirachy tree
class Order(models.Model):
order_name = models.CharField(max_length=100, unique=True)#, null=True, blank=True) #Unique name of order
created_by = models.ForeignKey(User, related_name='Project_created_by', on_delete=models.DO_NOTHING) #Person who created the order
created_at = models.DateTimeField(auto_now_add=True) #Date/Time order was created
def __str__(self):
return self.order_name
#For CE router definition. Required for all orders.
class Ce_Base(models.Model):
#Hardware models of router
ROUTER_MODELS = (
('CISCO2901', 'CISCO2901'),
('ISR4331', 'ISR4331'),
('CISCO1921', 'CISCO1921'),
('ISR4351', 'ISR4351'),
('ISR4451', 'ISR4451'),
('ISR4231', 'ISR4231'),
('ISR4431', 'ISR4431'),
('ISR4461', 'ISR4461'),
)
#Available regions in which the router can reside.
REGION = (
('1', '1'),
('2', '2'),
('3', '3'),
('4', '4'),
('5', '5'),
('6', '6'),
('7', '7'),
)
#Properties of CE Base
ce_hostname = models.CharField(max_length=15) #Hostname of router i.e. XXXXXXXXX
new = models.BooleanField() #Whether the router currently exists on the network
location = models.TextField(null=True, blank=True) #Address of the POP site/rack location. Required if new.
router_model = models.CharField(max_length=200, null=True, choices=ROUTER_MODELS) #Hardware model of the router i.e ISR4351. Required if new.
region = models.CharField(max_length=200, null=True, choices=REGION) #Region in which the router resides. Required if new.
#Foreign key to tie CE Base to an order
order_reference = models.ForeignKey(Order, null=True, on_delete=models.CASCADE) #Order reference
l3_interfaces = JSONField #Layer 3 interfaces
这是因为您没有在模型中添加新字段。你应该写的是:
l3_interfaces = JSONField()
此外,关于语义的快速说明:
makemigrations
不会更新您的数据库结构,它会生成迁移文件migrate
确实通过应用生成的文件更新了您的数据库结构