-1

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!

karthikr
  • 97,368
  • 26
  • 197
  • 188

2 Answers2

0

the name of table must be location_countryzone.

tafer
  • 24
  • 3
  • That shouldn't affect this problem though should it? I would assume that would only apply if db_table wasn't specified, and even if it wasn't, wouldn't django automatically figure that out anyways and search for location_countryzone instead and still fail? – Brennan Kastner Jun 20 '14 at 13:55
  • I agree, the real name of the table must be app_tablename. – elmonkeylp Jun 20 '14 at 14:11
  • Even if it should be that way I'm not in a position to change the view name, it was this way before I joined the project and it is my understanding they don't want it changed if it isn't necessary. I'm assuming it isn't required to be named this way, that is just the proper convention right? – Brennan Kastner Jun 20 '14 at 14:16
  • The documentation you just linked says what I am doing overrides what you posted, not that it must be app_tablename, therefore making what I have valid. It doesn't HAVE to be location_countryzone. – Brennan Kastner Jun 20 '14 at 15:40
0

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.