Minor tweaks, may it be useful for someone:

*q1 = hierarchy_query.alias('q1')**
**s = select(**
**    [q1.c.id, q1.c.level, q1.c.is_leaf, q1.c.connect_path],**
**    from_obj=q1**
**).alias('o')**
**MyObjExt = type('MyObjExt', (Base,), {'__table__': s})**
**
# objects :)
rs = object_session(self).query(**
**    MyObj, MyObjExt**
**).from_statement(**
**    hierarchy_query**
**).all()*

*for myobj, h in rs:**
**    if h.level == 1:**
**        print(repr(myobj))**
**    else:**
**        print("+" + "-" * 3 * (h.level - 1) + repr(myobj))*

It'll display a SAWarning, because I'm creating a class on-the-fly (and it may be created before), like this one:

*sqlalchemy/ext/declarative/clsregistry.py:159: SAWarning: This declarative base already contains a class with the same class name and module name as mypkg.db.MyObjExt, and will be replaced in the string-lookup table.**
**  existing.add_item(cls)*

I think it'll not be a problem, but warn me anyone if it can be.


Cheers,
Richard.


On 04/25/2013 09:56 AM, Richard Gerd Kuesters wrote:
Well, probably nevermind because I made a workaround that satisfies me (may not be elegant, but that's OK).

Basically, I created the "o" type a little different: "o = type('MyObjExt', (Base,), {'__table__':q.alias('q')})" and append it to the query like: "session.query(MyObj, o).from_statement(q).all()"

Okay, now it returns a typle, but at least I don't have to make a second query :)


Cheers,
Richard.

On 04/25/2013 09:41 AM, Richard Gerd Kuesters wrote:
Hi all,

I've been playing with "sqla_hierarchy" from https://github.com/marplatense/sqla_hierarchy .

The problem is: the returned query appends 3 columns: level (Integer), is_leaf (Boolean) and connect_path (pg ARRAY).

So far, so good. If I execute the query using "session.execute(q).fetchall()", it works like a charm. But, as we know, "session.execute" returns a RowProxy, not objects. Using "session.query(MyObj).from_statement(q).all()", I'm able to get my mapped objects, but without the extra columns that would make me very pleased (level, is_leaf, connect_path). Is there a way to get around this?

I have done testings using "o = type('MyObjExt', (MyObj,), {'__table__': q}" and them use it on the session.query, *but* it looses foreign key references - or, well, I don't know how to "explain" this to the mapper (?), since MyObj is polymorphic (probably the pitfall?).


Thanks for your time and help.

Best regards,
Richard.
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.



--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to