迁移后外键的 Django ID 不存在

发布于 2025-01-15 19:26:57 字数 2700 浏览 1 评论 0原文

我是 Django 新手,我正在尝试创建一个具有两个属性的“游戏”模型:

  • 一个多对一字段,其中游戏模型的多个实例与自定义用户模型的实例相关联。
  • 一个多对多领域,其中游戏模型的实例与单词的多个实例连接,单词模型的实例与游戏模型的多个实例连接。

我的 models.py 模型的顶部:

from django.db import models
from users.models import CustomUser
from django.contrib.postgres.fields import ArrayField

游戏模型:

class SortingGame(models.Model):
    user_current_player = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    field_words = models.ManyToManyField(Word, related_name="field_sorting_games")

单词模型:

class Word(models.Model):
    str_word = models.CharField(max_length=50,null=True)
    int_grade_level = models.IntegerField()
    arrint_phonemes = ArrayField(models.CharField(max_length=50),null=True)
    arrstr_graphemes = ArrayField(models.CharField(max_length=50),null=True)
    int_num_syllables = models.IntegerField()
    arrstr_syllables = ArrayField(models.CharField(max_length=50),null=True)

用户模型:

class CustomUser(AbstractBaseUser):
    # must have the following fields for django
    email = models.EmailField(verbose_name="email",max_length = 100,unique=True)
    username = models.CharField(max_length = 30, unique = True)
    date_joined = models.DateTimeField(verbose_name = "date_joined",auto_now_add=True)
    last_login = models.DateTimeField(verbose_name = "last_login",auto_now = True)
    is_admin = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default = False)
    is_staff = models.BooleanField(default = False)
    is_active = models.BooleanField(default = True)
    first_name = models.CharField(max_length=15, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    spelling_level = models.IntegerField(default=1, unique=False)
    time_played = models.IntegerField(default=0, unique=False)
    percent_correct = models.IntegerField(default=0, unique=False)

admin.py:

from django.contrib import admin
from .models import Word, SortingGame

admin.site.register(SortingGame)

当我运行 python3 manage.py makemigrationspython3 manage.py migrate 时,它不会抱怨,但是当我进入管理页面时我的 django 网站上写着psycopg2.errors.UndefinedColumn:关系“game_sortinggame”的列“user_current_player_id”不存在

这让我认为问题出在 SortingGame 中的 user_current_player (在我添加该属性之前它工作得很好),但我在不同的论坛上四处查看,看看可能出了什么问题,但我似乎无法弄清楚。我尝试从头开始使用新数据库,但它仍然抛出相同的异常。任何想法将不胜感激 - 谢谢!

I'm new to Django, and I'm trying to create a "game" model with two attributes:

  • A many-to-one field where multiple instances of the game model are associated with an instance of a custom user model.
  • A many-to-many field where instances of the game model are connected with multiple instances of words, and instances of the word model are connected with multiple instances of the game model

Top of my models.py model:

from django.db import models
from users.models import CustomUser
from django.contrib.postgres.fields import ArrayField

Game model:

class SortingGame(models.Model):
    user_current_player = models.ForeignKey(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    field_words = models.ManyToManyField(Word, related_name="field_sorting_games")

Word model:

class Word(models.Model):
    str_word = models.CharField(max_length=50,null=True)
    int_grade_level = models.IntegerField()
    arrint_phonemes = ArrayField(models.CharField(max_length=50),null=True)
    arrstr_graphemes = ArrayField(models.CharField(max_length=50),null=True)
    int_num_syllables = models.IntegerField()
    arrstr_syllables = ArrayField(models.CharField(max_length=50),null=True)

User model:

class CustomUser(AbstractBaseUser):
    # must have the following fields for django
    email = models.EmailField(verbose_name="email",max_length = 100,unique=True)
    username = models.CharField(max_length = 30, unique = True)
    date_joined = models.DateTimeField(verbose_name = "date_joined",auto_now_add=True)
    last_login = models.DateTimeField(verbose_name = "last_login",auto_now = True)
    is_admin = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default = False)
    is_staff = models.BooleanField(default = False)
    is_active = models.BooleanField(default = True)
    first_name = models.CharField(max_length=15, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    spelling_level = models.IntegerField(default=1, unique=False)
    time_played = models.IntegerField(default=0, unique=False)
    percent_correct = models.IntegerField(default=0, unique=False)

admin.py:

from django.contrib import admin
from .models import Word, SortingGame

admin.site.register(SortingGame)

When I run python3 manage.py makemigrations and python3 manage.py migrate, it doesn't complain, but when I go to the admin page of my django site it says psycopg2.errors.UndefinedColumn: column "user_current_player_id" of relation "game_sortinggame" does not exist.

This makes me think the issue is with user_current_player in SortingGame (it worked fine before I added that attribute), but I've looked around on different forums to see what might be going wrong and I can't seem to figure it out. I tried starting from scratch with a new database, and it's still throwing the same exception. Any ideas would be appreciated—thanks!

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

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

发布评论

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

评论(2

时光与爱终年不遇 2025-01-22 19:26:57

内森!
首先要确保您拥有在 settings.py 文件中的 INSTALLED_APPS 中创建 CustomUser 模型的应用程序。
如果是这样,请查看您定义了 CustomUser 的此文件夹(应用程序),以验证那里是否确实存在迁移文件夹。

我怀疑 Django 不知道这个应用程序(不在 INSTALLED_APPS 下),因此没有迁移它。因此,您的数据库未找到用户模型连接。

也就是说,我建议您保留 Django User 定义的帐户模型,并创建另一个与之有直接关系的模型来处理配置文件/游戏字段,例如拼写级别、percentage_ Correct 等。

这将使您的模型“关注点”以后更有条理。

Nathan!
First thing would be make sure that you have the app where CustomUser model is created in your settings.py file, at INSTALLED_APPS.
If so, please have a look at this folder (app) where you have CustomUser defined to verify if there is in deed a migrations folder there.

I suspect that Django in not aware of this app (not under INSTALLED_APPS) and therefore did not migrated it. So, your database is not finding the User Model connection.

That said, I would suggested you to keep your account model as defined by Django User and create another model with a direct relationship to it to deal with profile/game fields such as spelling level, percentage_correct and so on.

This would keep your Model "concerns" more organized later on.

哭了丶谁疼 2025-01-22 19:26:57

如果您在尝试使用之前确实进行了迁移 (python manage.py makemigrations -appname)

之后您还需要在 admin.py 中添加模块

from django.contrib import admin
from .models import *

admin.site.register(SortingGame)
... all other modules

if you did make a migrations before try to use (python manage.py makemigrations -appname)

Also after That you need to Add the module in your admin.py

from django.contrib import admin
from .models import *

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