我将库存模型移动到它自己的应用程序中,并重置所有迁移和数据库。python manage.py makemigrations
没有问题,但在迁移时出现错误。希望有人能给我指出一个方向,因为追溯似乎对我解决这个问题没有太大帮助。
*由于堆栈溢出限制,我删除了部分代码和回溯
Django version 4.0.2
回溯
Operations to perform:
Apply all migrations: admin, auth, contenttypes, inventory, item, project_site, requisition, sessions, transfer, users
Running migrations:
Applying contenttypes.0001_initial... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0001_initial... OK
...
Applying item.0001_initial... OK
Applying project_site.0001_initial...Traceback (most recent call last):
File "C:UsersBernardpelicansimrs-capstoneimrsmanage.py", line 22, in <module>
main()
File "C:UsersBernardpelicansimrs-capstoneimrsmanage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:UsersBernardpelicansimrs-capstoneenvlibsite-packagesdjangocoremanagement__init__.py", line
425, in execute_from_command_line
utility.execute()
...
File "C:UsersBernardpelicansimrs-capstoneenvlibsite-packagesdjangodbmigrationsoperationsmodels.py", line 92, in database_forwards
schema_editor.create_model(model)
File "C:UsersBernardpelicansimrs-capstoneenvlibsite-packagesdjangodbbackendsbaseschema.py", line 364, in create_model
if field.remote_field.through._meta.auto_created:
AttributeError: 'str' object has no attribute '_meta'
project_site/迁移/0001 _initial.py
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('item', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Cart',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('cartItemCount', models.PositiveIntegerField(default=0)),
],
),
migrations.CreateModel(
name='Site',
fields=[
...
('inventory_items', models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')),
('siteCart', models.ManyToManyField(blank=True, related_name='siteCart', through='project_site.Cart', to='item.Item')),
],
),
]
project_site/models.py
class Site(models.Model):
siteCart = models.ManyToManyField(
'item.Item',
through='Cart',
through_fields=('site', 'cartItem'),
blank=True,
related_name='siteCart')
inventory_items = models.ManyToManyField(
'item.Item',
through='inventory.Inventory',
through_fields=('site', 'item'),
blank=True,
related_name='inventory_items'
)
库存/models.py
class Inventory(models.Model):
item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
site = models.ForeignKey(Site, on_delete=models.CASCADE)
...
库存/迁移/0001 _initial.py
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('item', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Inventory',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('siteItemCount', models.PositiveIntegerField(default=0)),
('siteItemStatus', models.PositiveSmallIntegerField(choices=[(0, 'Above Threshold'), (1, 'Moderate'), (2, 'Low'), (3, 'Critical'), (4, 'Empty')], default=1)),
('siteItemTurnover', models.CharField(choices=[('S', 'Slow'), ('N', 'Normal'), ('F', 'Fast')], default='F', max_length=1)),
('siteItemMinThreshold', models.PositiveSmallIntegerField(default=0)),
('item', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='item.item')),
],
),
]
您有一个循环迁移依赖关系的问题,project_site
和inventory
相互依赖。如果你希望保持当前的应用/模型结构,你需要分解你的迁移文件,这样它们就可以在没有循环依赖的情况下被应用。
首先从Site
模型迁移中删除引用inventory
中的模型的ManyToManyField
project_site/迁移/0001 _initial.py:
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('item', '0001_initial'),
]
operations = [
...
migrations.CreateModel(
name='Site',
fields=[
...
# delete this line ('inventory_items', models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')),
],
),
]
然后添加初始project_site
迁移作为初始inventory
迁移的依赖项
库存/迁移/0001 _initial.py:
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('item', '0001_initial'),
('project_site', '0001_initial'),
]
...
然后在project_site
中创建一个新的迁移,添加ManyToManyField
,这现在应该是可能的,因为通过模型已经创建了
project_site/迁移/0002 _add_m2m.py:
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('project_site', '0001_initial'),
('inventory', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='Site',
name='inventory_items',
field=models.ManyToManyField(blank=True, related_name='inventory_items', through='inventory.Inventory', to='item.Item')
)
]