Aditya Sehgal
Aditya Sehgal

Reputation: 2883

(Python) Peewee - ForeignKey relation does not exist

I am using peewee with python and am having trouble accessing the attribute of the parent table using the foreign key

I have two tables Jobs and Tiles with the following schema

  class Job(BaseModel):
    id = PrimaryKeyField()
    block_id = IntegerField()
    min_lat = DoubleField()
    min_lng = DoubleField()
    max_lat = DoubleField()
    max_lng = DoubleField()
    city    = TextField()
    status  = IntegerField()
    #0 : not started
    #1 : ongoing
    #2 : download complete
    #3 : upload complete
    date_created  = DateTimeField()
    date_modified = DateTimeField()
    class Meta:
        order_by = ('id',)

class Tile(BaseModel):
    id = PrimaryKeyField()
    job = ForeignKeyField(Job)
    lat = DoubleField()
    lng = DoubleField()
    is_pending = BooleanField(default=True)
    filename = CharField(default='')
    date_created  = DateTimeField()
    date_modified = DateTimeField()
    class Meta:
        order_by = ('id',)

Tile table contains a job_id as a foreign key to Job table.

Several Tiles can belong to the same job.

In order to insert, a job is created and then tiles job_id field is set to job.id

....
         tile['job'] = job
         tile['lat'] = item.lat
         tile['lng'] = item.long

I am catching all the exceptions and no errors are thrown at this point.

Sometime later I try to access Job's city attribute using a Tile object

tiledesciptor.job.city

and peewee/sqlite throw

raise self.rel_model.DoesNotExist

I have just started using peewee but have gone through the documentation and example and cannot seem to figure out what I am doing wrong.

Any pointers?

Upvotes: 0

Views: 1410

Answers (1)

Aditya Sehgal
Aditya Sehgal

Reputation: 2883

Apologies. I figured out. I was using Select(id) and only returning the id field of the row I was trying to extract. Once I changed it to Select(), my code works.

Upvotes: 0

Related Questions