djangopostgresqlviewdjango-modelsdjango-syncdb

Django syncdb cannot find postgres View for ForeignKey relation


I'm trying to create a ForeignKey relation to a postgres view. This was originally working with a different model but now that I've created a second one it seems unable to create the new table.

The error I'm given is: DatabaseError: referenced relation "countryzone" is not a table

The Country model is defined as follows:

class Country(models.Model):
    code = models.CharField(
        db_column = 'countrycode',
        primary_key = True,
        max_length = 2,
    )
    name = models.CharField(
        max_length = 100,
        db_column = 'countryname',
        unique = True,
    )
    language_code = models.CharField(
        max_length = 8,
        null = True,
    )
    country_level = models.IntegerField()
    latitude = models.DecimalField(
        db_column = 'clat',
        decimal_places = 3,
        max_digits = 8,
        null = True,
    )
    longitude = models.DecimalField(
        db_column = 'clong',
        decimal_places = 3,
        max_digits = 8,
        null = True,
    )
    timezone = models.IntegerField()
    zone = models.CharField(max_length=1)
    transit_time = models.IntegerField()

    ## MANAGER
    objects = CountryManager()

    ## META DATA
    class Meta:
        db_table = 'countryzone'

My new model creates the ForeignKey the following way:

country = models.ForeignKey(
    to = 'location.Country',
    db_column = 'countrycode',
)

I'm referencing the Country model from a different class without any problems like so:

countrycode = models.ForeignKey(
    to = 'location.Country',
    db_column = "countrycode",
)

Any ideas where I might be going wrong or what I should look into to find my problem? Thanks!


Solution

  • Looks like syncdb can't handle this on it's own, I needed to use manage.py sqlall to get the correct query and run it myself. Once doing so the table was skipped over on future attempts of syncdb so then I was able to continue using it.