piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.46k stars 91 forks source link

using db_column_name with ForeignKey breaks objects(*prefetch) #1110

Closed jessemcl-flwls closed 1 month ago

jessemcl-flwls commented 1 month ago

Thank you for addressing this issue so quickly: https://github.com/piccolo-orm/piccolo/issues/1101

Sadly, this rabbit hole might go a bit deeper. With the above fix, I immediately ran into the next possibly related thing:

Again, if I have two tables with a foreign key relationship between them, and I specify the relationship using a custom db_column_name, eg:

class FooTable(Table, tablename="foo", schema="public"):
    id = Varchar(length=20, primary_key=True, null=False)
    name = Varchar(length=64, unique=True, null=False)

class BarTable(Table, tablename="bar", schema="public"):
    id = Varchar(length=20, primary_key=True, null=False)
    foo = ForeignKey(references=FooTable, null=False, db_column_name="foo_id")

Then when I try to query this relationship using prefetch:

bar = await BarTable.objects(BarTable.foo).where(BarTable.foo.id == "abc").first()

Then I get an error saying there is no matching column found.

If I leave out the prefetch, then it works without error, with the resulting dict containing the property:

  "foo": "abc"
jessemcl-flwls commented 1 month ago

hmmm, not sure how i raised this twice, apologies