Django initial migration error when using related queryset in a Modelform

I use a queryset in a modelform instance to populate choices. However, when I now want to run the initial project db migration it returns

django.db.utils.ProgrammingError: relation "core_category" does not exist LINE 1: ..._category"."name", "core_category"."fa_icon" FROM "core_cate...

I tried to use a try - except block to work-around this but it didn't change the situation. So how to overcome this? As of course the categories table will be non-existing when migrating the project initially.

# forms.py

class PollerForm(ModelForm):
    """
    A form to create new Pollers
    """
    # Get categories as choices, use try except
    # for initial migrations if table is not available yet
    try:
        CHOICES = Category.objects.all()
    except (OperationalError, ProgrammingError) as e:
        CHOICES = []

    # User instance will be passed in the view
    class Meta:
        model = Poller
        exclude = ('created_by',)

    category = forms.Select(choices=CHOICES) # Error raised here
# models.py

class Category(models.Model):
    """
    A table to store available categories
    """
    # Categories name
    name = models.CharField(max_length=20)

    # Fontawesome icon tag
    fa_icon = models.CharField(max_length=20)

    def __str__(self):
        return f'{self.name}'


class Poller(models.Model):
    """
    A table to store Pollers
    """
    # Unique identifier
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

    # Created at date
    created_at = models.DateTimeField(auto_now_add=True)

    # User who created the poller
    created_by = models.ForeignKey('user.UserProfile', on_delete=models.CASCADE, related_name='user')

    # The Poller's text
    text = models.CharField(max_length=250)

    # The pollers category
    category = models.ForeignKey('core.Category', on_delete=models.CASCADE, related_name='category')

    # Option one of the poller
    option_one = models.CharField(max_length=20)

    # Option two of the poller
    option_two = models.CharField(max_length=20)

    def __str__(self):
        return f'{self.text}'
Back to Top