Upgrading circular foreign keys from Peewee 2.7 to 3.13

24 views
Skip to first unread message

Stefano Menci

unread,
Aug 6, 2020, 12:12:14 PM8/6/20
to peewee-orm
I am upgrading an old app from Python 3.4 with Peewee 2.7.4 to Python 3.8 with Peewee 3.13.3.

The old code complains with AttributeError: Cannot use uninitialized Proxy.

# TempFile and PdfPageNumber have a circular foreign key dependency
# See http://peewee.readthedocs.org/en/latest/peewee/cookbook.html#circular-foreign-key-dependencies

PageNumberProxy = Proxy()

class TempFile(Model):
    png_page
= ForeignKeyField(PageNumberProxy, null=True)

class PdfPageNumber(Model):
    pdf_file
= ForeignKeyField(TempFile, backref='page_numbers')

PageNumberProxy.initialize(PdfPageNumber)

The link mentioned in the old comment is now broken. Instead I found this page that seems to be the one I'm interested on.

I changed the old code to this, it seems to be working well, but I would like a confirmation that it's the right thing to do:

class TempFile(PeeweeModel):
 png_page
= peewee.DeferredForeignKey('PdfPageNumber', null=True)


class PdfPageNumber(PeeweeModel):
 pdf_file
= peewee.ForeignKeyField(TempFile, backref='page_numbers')

Thanks,
Stefano

Charles Leifer

unread,
Aug 6, 2020, 12:50:25 PM8/6/20
to peewe...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "peewee-orm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to peewee-orm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/peewee-orm/6a25d8c3-f2a2-4c0a-9ec8-7b49a560f454o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages