orm.column_property()

23 views
Skip to first unread message

Julien Cigar

unread,
Jun 21, 2023, 5:12:54 AM6/21/23
to sqlal...@googlegroups.com
Hello,

I'm trying to add a column_property to recursively load and merge some
json column.

I have the following:
https://gist.github.com/silenius/1af1072abae5829f54584f1ea554e074 but I
cannot figure how can I replace the '5011' at line 11 (which is the
object primary key) with the "current" involved objet..?

For example when I'm doing dbsession.get(MyClass, 1234) I cannot figure
how reference the id in the column_property query

Any idea ?

Thanks!

--
Julien Cigar
Belgian Biodiversity Platform (http://www.biodiversity.be)
PGP fingerprint: EEF9 F697 4B68 D275 7B11 6A25 B2BB 3710 A204 23C0
No trees were killed in the creation of this message.
However, many electrons were terribly inconvenienced.
signature.asc

Mike Bayer

unread,
Jun 21, 2023, 8:36:05 AM6/21/23
to noreply-spamdigest via sqlalchemy


On Wed, Jun 21, 2023, at 5:12 AM, Julien Cigar wrote:
> Hello,
>
> I'm trying to add a column_property to recursively load and merge some
> json column.
>
> I have the following:
> https://gist.github.com/silenius/1af1072abae5829f54584f1ea554e074 but I
> cannot figure how can I replace the '5011' at line 11 (which is the
> object primary key) with the "current" involved objet..?
>
> For example when I'm doing dbsession.get(MyClass, 1234) I cannot figure
> how reference the id in the column_property query

well you wouldnt hardcode the id like that, you'd need to make your query correlated to the parent.

so it would select() from "ac" where "ac" is "aliased(class_)" , then correlate to the class


.where(
class_.id == ac.id
).


as far as how that integrates into the CTE and all that you'd have to arrange the geometry of the SELECT to work out correctly, I would start with something very simple and build outwards.

correlated subquery column_property at https://docs.sqlalchemy.org/en/20/orm/mapped_sql_expr.html#using-column-property

Julien Cigar

unread,
Jun 23, 2023, 4:48:35 AM6/23/23
to sqlal...@googlegroups.com

On Wed, Jun 21, 2023 at 08:35:36AM -0400, Mike Bayer wrote:
>
>
>
> On Wed, Jun 21, 2023, at 5:12 AM, Julien Cigar wrote:
> > Hello,
> >
> > I'm trying to add a column_property to recursively load and merge some
> > json column.
> >
> > I have the following:
> > https://gist.github.com/silenius/1af1072abae5829f54584f1ea554e074 but I
> > cannot figure how can I replace the '5011' at line 11 (which is the
> > object primary key) with the "current" involved objet..?
> >
> > For example when I'm doing dbsession.get(MyClass, 1234) I cannot figure
> > how reference the id in the column_property query
>
> well you wouldnt hardcode the id like that, you'd need to make your query correlated to the parent.
>
> so it would select() from "ac" where "ac" is "aliased(class_)" , then correlate to the class
>
>
> .where(
> class_.id == ac.id
> ).

Thank you Mike, it _almost_ work.
With (1) SQLAlchemy now generates (2). I'm still getting an error:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) invalid reference to FROM-clause entry for table "content"
LINE 4: WHERE content_1.id = content.id UNION ALL SELECT content.id ...
^
HINT: Perhaps you meant to reference the table alias "content_1".

This is because the WITH statement appears at the "top" rather than a
subselect (and cannot reference the outer table)

Any idea how could I transform (1) to generate (3) rather than (2) ?

(1) https://gist.github.com/silenius/43717848057d87ef7945c4d621fce5e8
(2) https://gist.github.com/silenius/34d3a319a52f8fe4fcd2b6418ca9e927
(3) https://gist.github.com/silenius/6d048f2b7819d39b70cc4d77237f9031

Thanks a lot!

Julien

>
>
> as far as how that integrates into the CTE and all that you'd have to arrange the geometry of the SELECT to work out correctly, I would start with something very simple and build outwards.
>
> correlated subquery column_property at https://docs.sqlalchemy.org/en/20/orm/mapped_sql_expr.html#using-column-property
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/400b6b72-f601-47c8-aa30-827240097eb8%40app.fastmail.com.
signature.asc

Julien Cigar

unread,
Jun 23, 2023, 6:17:29 AM6/23/23
to sqlal...@googlegroups.com
OK I found it! I added nesting=True to the cte() expression
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/fdxvwszr6d6k624egxak3ctr5wcu43daz4z6o7okhikglq57to%40puqqqclybeig.
signature.asc

Mike Bayer

unread,
Jun 23, 2023, 9:29:56 AM6/23/23
to noreply-spamdigest via sqlalchemy
Hi Julien -

OK, if we're going to do one of your big queries, can we please start fresh, make a new discussion at https://github.com/sqlalchemy/sqlalchemy/discussions , give me **really really succinct** models + the query you want, etc.

the google groups thing here is not exactly going away but it's an antiquated interface at this point.
> https://groups.google.com/d/msgid/sqlalchemy/fdxvwszr6d6k624egxak3ctr5wcu43daz4z6o7okhikglq57to%40puqqqclybeig.
>
> Attachments:
> * signature.asc

Julien Cigar

unread,
Jun 23, 2023, 9:38:21 AM6/23/23
to sqlal...@googlegroups.com

On Fri, Jun 23, 2023 at 09:29:27AM -0400, Mike Bayer wrote:
>
> Hi Julien -

Hi Mike,

>
> OK, if we're going to do one of your big queries, can we please start fresh, make a new discussion at https://github.com/sqlalchemy/sqlalchemy/discussions , give me **really really succinct** models + the query you want, etc.
>
> the google groups thing here is not exactly going away but it's an antiquated interface at this point.
>

I think you missed one message, I added nesting=True to the cte()
expression and it does what I want :)

Next time I'll use https://github.com/sqlalchemy/sqlalchemy/discussions

Thanks!

Julien
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/1a27eba0-7528-4c73-be98-d3cf27c23b10%40app.fastmail.com.
signature.asc

Mike Bayer

unread,
Jun 23, 2023, 10:54:54 AM6/23/23
to noreply-spamdigest via sqlalchemy


On Fri, Jun 23, 2023, at 9:38 AM, Julien Cigar wrote:
> On Fri, Jun 23, 2023 at 09:29:27AM -0400, Mike Bayer wrote:
>>
>> Hi Julien -
>
> Hi Mike,
>
>>
>> OK, if we're going to do one of your big queries, can we please start fresh, make a new discussion at https://github.com/sqlalchemy/sqlalchemy/discussions , give me **really really succinct** models + the query you want, etc.
>>
>> the google groups thing here is not exactly going away but it's an antiquated interface at this point.
>>
>
> I think you missed one message, I added nesting=True to the cte()
> expression and it does what I want :)

hooray! onto the weekend then
> https://groups.google.com/d/msgid/sqlalchemy/kqhr4bcnkfvnf6zb4sgxngfyngea35hoaxvrn46hgbg7bnqpqq%40l6ac7xf2ohj4.
>
> Attachments:
> * signature.asc
Reply all
Reply to author
Forward
0 new messages