Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

Model.__getattr__ handling of pk alias #139

Closed Jaymon closed 3 years ago

Jaymon commented 3 years ago

we have a line in __getattr__ like this:

if k != "pk"

It would be better if that line was updated to check all the primary key aliases:

if k in schema.fields[schema.pk_name].names
Jaymon commented 3 years ago

So I totally misunderstood what this code was supposed to do. Usually, when we remove the primary key, we do this:


class MyOrm(Orm):
    _id = None

and so _id is always None:

o = MyOrm()
o._id # None

and we want pk to do the same thing since pk is treated as special (an alias to the primary key, whatever it's named) in the code, so we want this to work:

o = Orm()
o._id # None
o.pk # None

But since _id doesn't exist, pk doesn't exist, and an AttributeError will be raised, so the k != "pk" just makes sure pk is treated like _id