keredson / peewee

a small, expressive orm -- supports postgresql, mysql and sqlite
http://docs.peewee-orm.com/
MIT License
13 stars 4 forks source link

QoL Patch on MagicAll to always default to select all columns. #26

Open logannc opened 6 years ago

logannc commented 6 years ago

By default, Model.select() only selects all columns as a standalone query. If used like Model.select().join(OtherModel.select(), on = predicate), OtherModel will only select the primary key.

This changes MagicAll to specify that we want cls.select(cls) which means select cls.*, even in subqueries.

Changing .select might be trivial but I haven't actually found where it changes the select arguments.

keredson commented 6 years ago

how will this work w/ subqueries in in statements?

logannc commented 6 years ago

If you mean,

Post.ALL.where(Post.user.in_(User.ALL.where(User.created_at > yesterday)))

then it won't.

You'd need Post.ALL.where(Post.user.in_(User.select().where(User.created_at > yesterday))) or Post.ALL.where(Post.user.in_(User.select(User.id).where(User.created_at > yesterday))).

EDIT:

or Post.ALL.join(User).where(User.created_at > yesterday) or sub = User.ALL.where(User.created_at > yesterday); Post.ALL.join(sub, on = (Post.user == sub.c.user_id))

The implicit primary-key only select feels like an antipattern as the query object no longer represents the sql in a context independent way. Besides, the subquery-in pattern is often an antipattern where a join is semantically equivalent and often faster if the DB isn't sufficiently smart to optimize it.