Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
I took 30 seconds to reformat your code. Please take the time next time. –  rnevius Jun 20 at 13:53
    
@rnevius I figured it wasn't a big deal since it was still easily readable but thanks! –  Brennan Kastner Jun 20 at 13:56
    
Did you add the meta property db_table after you did a syncdb ? –  karthikr Jun 20 at 13:56
    
@karthikr I did not, I've had it in there since before I even tried creating the new model. –  Brennan Kastner Jun 20 at 13:58

2 Answers 2

the name of table must be location_countryzone.

share|improve this answer
    
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 at 13:55
    
I agree, the real name of the table must be app_tablename. –  elmonkeylp Jun 20 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 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 at 15:40

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.