Я пишу миграцию вручную, чтобы быстро добавить в новую среду. Когда я пытаюсь создать новый объект модели Operator, я получаю сообщение об ошибке с пустым идентификатором. Я пытался установить managed = False
в мета-версии и поддельной Operator
модели, но ничего из этого не дало результатов. Что не так с моим кодом?
Моя модель:
class Operator(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=256)
class Meta:
db_table = '"mts_market"."operator"'
managed = False
def __str__(self):
return self.name
Моя миграция:
from django.db import migrations
def forwards_func(apps, schema_editor):
product = apps.get_model("landings", "Product")
merchant = apps.get_model("landings", "Merchant")
partner = apps.get_model("landings", "Partner")
operator = apps.get_model("landings", "Operator")
tariff = apps.get_model("landings", "Tariff")
db_alias = schema_editor.connection.alias
partner = partner.objects.using(db_alias).get(name="binding")
merchant = merchant.objects.using(db_alias).get(name="dc")
operator.objects.using(db_alias).bulk_create([
operator(name="test")
])
operator = operator.objects.using(db_alias).get(name="test")
product.objects.using(db_alias).bulk_create([
product(name="mirrorcheck", merchant=merchant,
mnp_service_name_card="test",
partner=partner,
operator=operator,
sub_service_name_card="test",
has_many_subs=True, category='retail',
])
product = product.objects.using(db_alias).get(name="mirrorcheck",
partner=partner.id, merchant=merchant.id, operator=operator.id)
tariff.objects.using(db_alias).bulk_create([
tariff(name='card_subscription', value=100,
product=product, mnp_instance_details={},
sub_instance_details={"amount": 29900},
)
])
partner = partner.objects.using(db_alias).get(name="upfront")
product.objects.using(db_alias).bulk_create([
product(name="mirrorcheck", merchant=merchant,
mnp_service_name_card="test",
partner=partner,
operator=operator,
sub_service_name_card="zenit_bank_test",
has_many_subs=True, category='retail',
])
product = product.objects.using(db_alias).get(name="mirrorcheck",
partner=partner.id, merchant=merchant.id, operator=operator.id)
tariff.objects.using(db_alias).bulk_create([
tariff(name='card_subscription', value=29900,
product=product, mnp_instance_details={},
sub_instance_details={"amount": 29900},
)
])
def reverse_func(apps, schema_editor):
operator = apps.get_model("landings", "Operator")
product = apps.get_model("landings", "Product")
tariff = apps.get_model("landings", "Tariff")
partner = apps.get_model("landings", "Partner")
db_alias = schema_editor.connection.alias
partner = partner.objects.using(db_alias).get(name="binding")
operator = operator.objects.using(db_alias).get(name="test")
product = product.objects.using(db_alias).get(name="mirrorcheck", operator=operator.id, partner=partner.id)
tariff.objects.using(db_alias).get(name="card_subscription", product=product.id).delete()
product.delete()
partner = partner.objects.using(db_alias).get(name="upfront")
product = product.objects.using(db_alias).get(name="mirrorcheck", operator=operator.id, partner=partner.id)
tariff.objects.using(db_alias).get(name="card_subscription", product=product.id).delete()
product.delete()
operator.delete()
class Migration(migrations.Migration):
dependencies = [('landings', '0026_product_operator')]
operations = [
migrations.RunPython(forwards_func, reverse_func),
]
Ошибка:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
psycopg2.IntegrityError: ОШИБКА: нулевое значение в столбце "id" нарушает ограничение NOT NULL
DETAIL: Ошибочная строка содержит (null, test).