从模型保留ID中删除继承

发布于 2025-02-13 21:48:38 字数 903 浏览 0 评论 0原文

我有两个类,其中一个

class Drink(models.Model):
   ....
class Juice(Drink)
  ....

现在是另一个类别,这是一个错误,我想删除继承,并使果汁模型为正常模型juice(models.model)> 。但是,我想保留超级阶级中的ids的顺序。由于继承,超级类具有自动ID字段,子类具有指针字段(draind_ptr_id)。

现在,当我更改语法时,现在会发生什么是Django试图在Juice模型中添加自动ID字段,并希望我设置默认值。显而易见的问题是,我需要将超模的顺序复制到子类模型的自动ID字段中,而我不能只添加默认值。第二个问题 - 对我来说很奇怪 - 子类已经具有自动插入ID字段,因此新创建的字段与旧ID字段之间存在冲突。

我试图遵循本文中给出的建议:删除模型继承并将ID保留在新创建的自动赛中,这几乎是一个类似的问题。

但是,当我遵循此建议时,我会得到django.core.exceptions.fielderror:与ID的类冲突中的本地字段u'id'。我试图通过更改名称来调整手动迁移,如果id id_second字段,但随后我发现id_second字段是在一个完全弄乱事情的主要键中。因此,经过相当多的手术,我回到了我开始的地方。我有很多数据,因此不是从头开始的选择。

有没有办法去除继承并保留外国钥匙?如果我需要提供更多信息或更清楚地说明,请LMK。事先感谢您的任何帮助。

I have a two classes, one of which inherits from the other

class Drink(models.Model):
   ....
class Juice(Drink)
  ....

Now, this was a mistake and I would like to remove the inheritance and have the Juice model to be a normal model Juice(models.Model). Yet, I would like to retain the sequence of IDs that are in the superclass. Due to the inheritance, the superclass has an auto ID field and the subclass has a pointer field (drink_ptr_id).

What happens now when I just change the syntax is that django tries to add an auto ID field to the juice model and wants me to set a default value. The obvious problem is that I need the sequence of the supermodel to be copied into the auto ID field of the subclass model and I cannot just add a default value. A second problem - which is rather strange to me - is that the subclass already has an auto-incremented ID field, so there is a clash between the newly created field and the old id field.

I tried to follow the advice given in this post: Remove model inheritance and keep id in newly created autofield, which is pretty much a similar problem.

But when I follow this advice I get a django.core.exceptions.FieldError: Local field u'id' in class clashes with ID. I tried to adapt the manual migration by changing the name if the ID to ID_second field, but then I got an error that the ID_second field is within a primary key which completely messed things up. So after quite a bit of a surgery I am back to where I started. I have quite a lot of data so just starting from scratch is not an option.

Is there a way to remove the inheritance and retaining the foreign keys? If I need to provide more info or explain clearer please LMK. Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2025-02-20 21:48:38

更改您的模型。

class Juice(models.Model):
    drink_ptr_id = models.AutoField(primary_key=True)

通过makemigrations制造了新的Shema。

在新模式中的makemigration之后,应仅为Alterfield,而不是Addfield或DeleteField。

迁移。

之后,您可以不用其他饮料使用果汁。 )

Change you model.

class Juice(models.Model):
    drink_ptr_id = models.AutoField(primary_key=True)

made new shema through makemigrations.

After Makemigrations in new schema should be only AlterField, not the AddField or DeleteField.

migrate.

After that you can use your Juice without other Drink. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文