gpt4 book ai didi

django-south - 使用 South 迁移重父类时,_ptr 使用什么值?

转载 作者:行者123 更新时间:2023-12-04 01:40:02 25 4
gpt4 key购买 nike

我有两个类,其中一个是另一个类的后代,我想让它们都是同一个基类的兄弟类。

前:

from django.db import models

class A(models.Model):
name = models.CharField(max_length=10)

class B(models.Model):
title = models.CharField(max_length=10)

后:
from django.db import models

class Base(models.Model):
name = models.CharField(max_length=10)

class A(Base):
pass

class B(Base):
title = models.CharField(max_length=10)

当我生成架构迁移时,这是输出,包括我对问题的回答:
+ Added model basetest.Base
? The field 'B.a_ptr' does not have a default specified, yet is NOT NULL.
? Since you are removing this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception.
? Please select a choice: 3
- Deleted field a_ptr on basetest.B
? The field 'B.base_ptr' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? Please select a choice: 2
? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> 37
+ Added field base_ptr on basetest.B
? The field 'A.id' does not have a default specified, yet is NOT NULL.
? Since you are removing this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception.
? Please select a choice: 3
- Deleted field id on basetest.A
? The field 'A.name' does not have a default specified, yet is NOT NULL.
? Since you are removing this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? 3. Disable the backwards migration by raising an exception.
? Please select a choice: 3
- Deleted field name on basetest.A
? The field 'A.base_ptr' does not have a default specified, yet is NOT NULL.
? Since you are adding this field, you MUST specify a default
? value to use for existing rows. Would you like to:
? 1. Quit now, and add a default to the field in models.py
? 2. Specify a one-off value to use for existing columns now
? Please select a choice: 2
? Please enter Python code for your one-off default value.
? The datetime module is available, so you can do e.g. datetime.date.today()
>>> 73
+ Added field base_ptr on basetest.A
Created 0002_auto__add_base__del_field_b_a_ptr__add_field_b_base_ptr__del_field_a_i.py. You can now apply this migration with: ./manage.py migrate basetest

我不知道如何回答有关 B.base_ptr 和 A.base_ptr 的默认值的问题。我给出的任何常量都会导致迁移在运行时失败,输出如下:
FATAL ERROR - The following SQL query failed: CREATE TABLE "_south_new_basetest_a" ()
The error was: near ")": syntax error
RuntimeError: Cannot reverse this migration. 'B.a_ptr' and its values cannot be restored.

顺便说一下,这是我使用 sqlite3 时的结果。使用 Postgres 给出了这样的结果:
FATAL ERROR - The following SQL query failed: ALTER TABLE "basetest_a" ADD COLUMN "base_ptr_id" integer NOT NULL PRIMARY KEY DEFAULT 73;
The error was: could not create unique index "basetest_a_pkey"
DETAIL: Key (base_ptr_id)=(73) is duplicated.

Error in migration: basetest:0002_auto__add_base__del_field_b_a_ptr__add_field_b_base_ptr__del_field_a_i
IntegrityError: could not create unique index "basetest_a_pkey"
DETAIL: Key (base_ptr_id)=(73) is duplicated.

我应该为 base_ptr 使用什么值来使此迁移工作?谢谢!

最佳答案

base不会自行实例化,您可以使用 abstract = True 轻松解决问题支持 class Meta .

示例代码:

from django.db import models

class Base(models.Model):
name = models.CharField(max_length=10)
class Meta:
abstract = True

class A(Base):
pass

class B(Base):
title = models.CharField(max_length=10)

关于django-south - 使用 South 迁移重父类时,_ptr 使用什么值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16487977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com