You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PostgresQL supports composite types (e.g. row types) as function arguments, but I can't figure out how to use a model as an argument of a peewee.fn.func.
Say we have this schema
CREATETABLEIF NOT EXISTS post (
id INTEGERNOT NULLPRIMARY KEY,
votes INTEGERNOT NULL,
posted TIMESTAMPTZNOT NULL DEFAULT CURRENT_TIMESTAMP
);
and a function
CREATE OR REPLACEFUNCTIONscore(post)
RETURNS double precisionAS $$ SELECT $1.votes / EXTRACT(EPOCH FROM now() - $1.posted); $$
LANGUAGE SQL
STABLE;
we want to build this query:
SELECT id, score(post) FROM post;
How do we approach this?
I cannot use the model Post as an argument to peewee.fn.score:
Post.select(Post.id, fn.score(Post))
# => peewee.ProgrammingError: can't adapt type 'ModelBase'
even SQL doesn't work unless I force an alias on the model:
Post.select(Post.id, SQL("score(post)"))
# peewee.ProgrammingError: column "post" does not exist (because 'FROM "post" AS "t1"' in the constructed query)
Post_=Post.alias("post")
Post_.select(Post_.id, SQL("score(post)"))
# This one works.
I'm currently using the workaround above. Is there any better way to achieve this? Sorry if I missed something in the docs, I think I scoured every corner. (And sorry for the barrage of issues I've opened recently.)
The text was updated successfully, but these errors were encountered:
PostgresQL supports composite types (e.g. row types) as function arguments, but I can't figure out how to use a model as an argument of a
peewee.fn.func
.Say we have this schema
and a function
we want to build this query:
How do we approach this?
I cannot use the model
Post
as an argument topeewee.fn.score
:even
SQL
doesn't work unless I force an alias on the model:I'm currently using the workaround above. Is there any better way to achieve this? Sorry if I missed something in the docs, I think I scoured every corner. (And sorry for the barrage of issues I've opened recently.)
The text was updated successfully, but these errors were encountered: