Re: [sqlalchemy] Can't locate strategy for ... (('lazy', 'joined'),)

2015-05-21 Thread Mike Bayer



On 5/21/15 3:56 PM, Russ wrote:


nope.  I'd need a complete, self-contained and succinct example I
can run, thanks


Ok, thanks.  This is a beefy one so that will be extremely tricky to 
extract.  I had hoped that the combo of lazy+joined would have been a 
clear indicator since they are opposite loading strategies.


the word lazy there is the name of the field. It is the internal 
equivalent to the lazy argument on relationship:


http://docs.sqlalchemy.org/en/rel_1_0/orm/relationship_api.html?highlight=relationship#sqlalchemy.orm.relationship.params.lazy

that said, here is exactly what will cause your error. Any of the 
attribute names defined_items, child_product or number are not in 
fact bound to a relationship(), and instead refer to a Column-mapped 
attribute.  Looks a lot like number here is a Column.  Is that the 
case?  You can't call joinedload for a column attribute.



--
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] relationsip(): In Parent or Child Class?

2015-05-21 Thread Mike Bayer



On 5/21/15 4:32 PM, Rich Shepard wrote:

  There are a number of many-to-one table/class relationships in the
application. In the many class I use ForeignKey() to relate that 
column to

the appropriate 'one' class and column.

  Reading the ORM tutorial tells me that the relationship() function 
can be

in either table. I can specify the backref from the many class to the one
class or define it as a child in the one class with the backref to the 
many

class.

  Is one placement preferred over the other? If not, what criteria are 
used

to determine where the relationship() function should be placed?
I think the best use case is to put it on both, using back_populates.  
This is the focus of current documentation: 
http://docs.sqlalchemy.org/en/rel_1_0/orm/backref.html


It's more verbose but I think it's clearer and functionally this is what 
backref does behind the scenes in any case.









Rich


--
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Can't locate strategy for ... (('lazy', 'joined'),)

2015-05-21 Thread Russ
Yes, 'number' is a column, as you surmised.  When I drop that from the path 
it works fine.  The only remaining problem is/was that this ends up loading 
in every field in the child_product table, and this includes a potentially 
massive BSON column (and more).

After looking into this, I've now learned about deferred, defer, undefer, 
loadonly, etc.  This seems to be the correct way to manage this, and it 
appears to be working fine:

q = q.options(sa.orm
  .joinedload(defined_items)
  .joinedload(child_product)
  .load_only(number)
  )

Thanks for pointing me in the right direction!  This page had the info I 
needed:
http://docs.sqlalchemy.org/en/latest/orm/loading_columns.html

Russ


On Thursday, May 21, 2015 at 4:01:16 PM UTC-4, Michael Bayer wrote:

  

 On 5/21/15 3:56 PM, Russ wrote:
  
  nope.  I'd need a complete, self-contained and succinct example I can 
 run, thanks


  Ok, thanks.  This is a beefy one so that will be extremely tricky to 
 extract.  I had hoped that the combo of lazy+joined would have been a clear 
 indicator since they are opposite loading strategies.
  

 the word lazy there is the name of the field. It is the internal 
 equivalent to the lazy argument on relationship:  


 http://docs.sqlalchemy.org/en/rel_1_0/orm/relationship_api.html?highlight=relationship#sqlalchemy.orm.relationship.params.lazy

 that said, here is exactly what will cause your error. Any of the 
 attribute names defined_items, child_product or number are not in 
 fact bound to a relationship(), and instead refer to a Column-mapped 
 attribute.  Looks a lot like number here is a Column.  Is that the case?  
 You can't call joinedload for a column attribute.   


  

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] relationsip(): In Parent or Child Class?

2015-05-21 Thread Rich Shepard

On Thu, 21 May 2015, Mike Bayer wrote:


I think the best use case is to put it on both, using back_populates.
This is the focus of current documentation:
http://docs.sqlalchemy.org/en/rel_1_0/orm/backref.html

It's more verbose but I think it's clearer and functionally this is what 
backref does behind the scenes in any case.


Mike,

  OK. I'm carefully working through the ORM tutorial and cleaning code as I
go.

Thanks,

Rich


[sqlalchemy] relationsip(): In Parent or Child Class?

2015-05-21 Thread Rich Shepard

  There are a number of many-to-one table/class relationships in the
application. In the many class I use ForeignKey() to relate that column to
the appropriate 'one' class and column.

  Reading the ORM tutorial tells me that the relationship() function can be
in either table. I can specify the backref from the many class to the one
class or define it as a child in the one class with the backref to the many
class.

  Is one placement preferred over the other? If not, what criteria are used
to determine where the relationship() function should be placed?

Rich


Re: [sqlalchemy] relationsip(): In Parent or Child Class?

2015-05-21 Thread Rich Shepard

On Thu, 21 May 2015, Mike Bayer wrote:


I think the best use case is to put it on both, using back_populates.


  Validation check: am I correctly using relationship() in the following set
of three tables? (N.B. Other columns removed for clarity and space saving.)

class Agencies(Base):
__tablename__ = 'agencies'
name = Column(Unicode(48), Sequence('name_seq'), primary_key = True)
...
child1 = relationship('AgencyUnits', back_populates = 'agency', cascade = 
'all, delete, delete-orphan')
child2 = relationship('Permits', back_populates = 'agency', cascade = 'all, 
delete, delete-orphan')
child3 = relationship('Inspect', back_populates = 'agency', cascade = 'all, 
delete, delete-orphan')

class AgencyUnits(Base):
__tablename__ = 'agencyUnits'
name = Column(Unicode(48), Sequence('name_seq'), primary_key = True)
parent_name = Column(Unicode(48), nullable = False, ForeignKey(\
'agencies.name'))
parent_id = relationship(Agency, back_populates = 'units')
...
child = relationship(AgencyContacts, back_populates = 'units', cascade = 
'all, delete, delete-orphan')

class AgencyContacts(Base):
__tablename__ = 'agencyContacts'
contact_id = Column(Integer, Sequence('contact_id_seq'), primary_key=True)
...
unit_id = Column(Unicode(48), ForeignKey('agencyUnits.name'))
parent = relationship(AgencyUnits, back_populates = 'contacts')

Rich




[sqlalchemy] How to use make_transient() to duplicate an SQLAlchemy mapped object?

2015-05-21 Thread c.buhtz
I opened a questions with example (pseudo) code on stackoverflow for
that.
http://stackoverflow.com/questions/30287042/how-to-use-make-transient-to-duplicate-an-sqlalchemy-mapped-object

I know the question how to duplicate or copy a SQLAlchemy mapped object
was asked a lot of times. The answer always depends on the needs or how
duplicate or copy is interpreted. This is a specialized version of
the question because I got the tip to use make_transient() for that.

But I have some problems with that. I don't really know how to handle
the primary key (PK) here. In my use cases the PK is always
autogenerated by SQLA (or the DB in background). But this doesn't
happen with a new duplicated object.

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Can't locate strategy for ... (('lazy', 'joined'),)

2015-05-21 Thread Mike Bayer



On 5/21/15 3:25 PM, Russ wrote:

I have a query I am running where sqlalchemy is throwing this exception:

Exception: can't locate strategy for class 
'sqlalchemy.orm.properties.ColumnProperty' (('lazy', 'joined'),)


What causes this is the addition of this joinedload_all option to a 
query (q):


q = q.options(sa.orm.joinedload_all(defined_items.child_product.number))

The intent of adding that is to try and avoid emitting sql when 
accessing foo.defined_items[x].child_product.number, as there are many 
defined_items.


There are several other joined loads on this query that don't have an 
issue.  Any ideas on what would be causing this particular problem?
nope.  I'd need a complete, self-contained and succinct example I can 
run, thanks.






--
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+unsubscr...@googlegroups.com 
mailto:sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com 
mailto:sqlalchemy@googlegroups.com.

Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


--
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Can't locate strategy for ... (('lazy', 'joined'),)

2015-05-21 Thread Russ
I have a query I am running where sqlalchemy is throwing this exception:

Exception: can't locate strategy for class 
'sqlalchemy.orm.properties.ColumnProperty' (('lazy', 'joined'),)

What causes this is the addition of this joinedload_all option to a query 
(q):

q = q.options(sa.orm.joinedload_all(defined_items.child_product.number))

The intent of adding that is to try and avoid emitting sql when accessing 
foo.defined_items[x].child_product.number, as there are many defined_items.

There are several other joined loads on this query that don't have an 
issue.  Any ideas on what would be causing this particular problem?

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Can't locate strategy for ... (('lazy', 'joined'),)

2015-05-21 Thread Russ


 nope.  I'd need a complete, self-contained and succinct example I can run, 
 thanks


Ok, thanks.  This is a beefy one so that will be extremely tricky to 
extract.  I had hoped that the combo of lazy+joined would have been a clear 
indicator since they are opposite loading strategies.

Digging through the relationship definitions I thought it might be because 
the child_product relationship is explicitly declared with lazy = True 
('select') and (since I don't do that on other properties) I thought 
perhaps that was conflicting with the later joinedload request and 
confusing the strategy lookup.  However, I see that True/select is the 
default, and I explicitly request joinedload at query time on other 
relationships/properties without issue, so that can't be it.  I'm stumped 
again.



 




  -- 
 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 javascript:.
 To post to this group, send email to sqlal...@googlegroups.com 
 javascript:.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.


  

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.