Re: [sqlalchemy] Eager load extra columns in many to many relationships

2019-01-21 Thread Harshvardhan Gupta
Hi, thanks for the reply. 

>From what I understand , your example shows me filtering through a many to 
many relationship. However I was looking for explicitly also loading the 
extra field in the pivot table. 
In your example, will CorporateApplications.foo also be loaded in the 
result ? 



On Monday, 21 January 2019 20:38:52 UTC+5:30, Mike Bayer wrote:
>
> On Sun, Jan 20, 2019 at 7:50 AM Harshvardhan Gupta 
> > wrote: 
> > 
> > I have two tables, Corporates and Users , each corporate can be 
> reviewing multiple users, and each user can apply to multiple corporates. 
> > 
> > My models are defined as: 
> > 
> > class Corporate(Dictifiable, db.Model): 
> > 
> > 
> > 
> > __tablename__ = 'corporate' 
> > 
> > 
> > 
> > 
> > id = Column(Integer, Sequence('corporate_id_seq'), primary_key=True) 
> > 
> > name = Column(String(1024), nullable=False) 
> > 
> > 
> > 
> > 
> > admins = db.relationship('User', secondary='corporate_admin') 
> > 
> > tests = db.relationship('Test', secondary='corporate_test') 
> > 
> > applicants = db.relationship('User', 
> secondary='corporate_applicants') 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > class User(Dictifiable, db.Model, UserMixin): 
> > 
> > 
> > __tablename__ = 'user' 
> > 
> > 
> > 
> > 
> > applicant_for = db.relationship('Corporate', 
> > 
> > secondary='corporate_applicants') 
> > 
> > 
> > My many to many table is defined as: 
> > 
> > class CorporateApplicants(Dictifiable, db.Model): 
> > 
> > 
> > 
> > __tablename__ = 'corporate_applicants' 
> > 
> > 
> > 
> > 
> > user_id = Column(Integer, ForeignKey('user.id'), primary_key=True) 
> > 
> > corporate_id = Column(Integer, ForeignKey('corporate.id'), 
> > 
> >   primary_key=True) 
> > 
> > 
> > 
> > 
> > # i want to access this 
> > 
> > type = Column(db.Enum(ApplicationStatusTypes), nullable=True) 
> > 
> > 
> > My queries are usually done like this: 
> > 
> > Corporate.query .join(Corporate.applicants)# SQLAlchemy magic will make 
> it join through the many to many 
> .options(contains_eager(Corporate.applications)) .all() 
> > 
> > But this way I am unable to access the type field (for each user, 
> corporate combination it will be either "shortlist","hired","reject". 
> > 
> > It is not possible to run a loop and then fetch each value separately 
> due to the high number of applicants. 
> > 
> > How can I access this extra column while querying a many to many 
> relationship in an eager manner. 
>
> there's still magic that will help you but as always, explicit 
> approach will get you there, you need to name the entity you want to 
> query: 
>
> sess.query(Corporate).join(CorporateApplicants).join(User).filter(CorporateApplications.foo
>  
>
> == 'bar') 
>
> if those joins fail that there is too much ambiguity, or just as a 
> better practice in general, build some extra relationships  so you can 
> use them: 
>
> sess.query(Corporate).join(Corporate.applicant_associations).join(CorporateApplicant.user).filter(CorporateApplications.foo
>  
>
> == 'bar') 
>
>
>
>
> > 
> > -- 
> > 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 post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

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


[sqlalchemy] Eager load extra columns in many to many relationships

2019-01-20 Thread Harshvardhan Gupta


I have two tables, Corporates and Users , each corporate can be reviewing 
multiple users, and each user can apply to multiple corporates. 

My models are defined as:
class Corporate(Dictifiable, db.Model):



__tablename__ = 'corporate'




id = Column(Integer, Sequence('corporate_id_seq'), primary_key=True)

name = Column(String(1024), nullable=False)




admins = db.relationship('User', secondary='corporate_admin')

tests = db.relationship('Test', secondary='corporate_test')

applicants = db.relationship('User', secondary='corporate_applicants')







class User(Dictifiable, db.Model, UserMixin):


__tablename__ = 'user'




applicant_for = db.relationship('Corporate',

secondary='corporate_applicants')


My many to many table is defined as: 
class CorporateApplicants(Dictifiable, db.Model):



__tablename__ = 'corporate_applicants'




user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)

corporate_id = Column(Integer, ForeignKey('corporate.id'),

  primary_key=True)




# i want to access this

type = Column(db.Enum(ApplicationStatusTypes), nullable=True)


My queries are usually done like this:

Corporate.query .join(Corporate.applicants)# SQLAlchemy magic will make it join 
through the many to many .options(contains_eager(Corporate.applications)) .all()

But this way I am unable to access the type field (for each user, corporate 
combination it will be either "shortlist","hired","reject". 

It is not possible to run a loop and then fetch each value separately due 
to the high number of applicants. 

How can I access this extra column while querying a many to many 
relationship in an eager manner.

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


Re: [sqlalchemy] Polymorphic AbstractConcreteBase , eager load , conditions , etc

2018-03-07 Thread Harshvardhan Gupta
Calling configure_mappers() fixes the  issue. 

However, the next thing I tried was to do joined load ( for eager loading ) 
, and it fails. 

This is what I tried : 


order = db.session.query(Order).join(Order.items.of_type(order_items)).
options(joinedload(Order.items.of_type(order_items))).all() 




This returns an error: 

InvalidRequestError: Detected unaliased columns when generating joined load.  
Make sure to use aliased=True or flat=True when using joined loading with 
with_polymorphic().



So I added aliased= True in my polymorphic union, and when i query, i the same 
error: 


InvalidRequestError: Detected unaliased columns when generating joined load.  
Make sure to use aliased=True or flat=True when using joined loading with 
with_polymorphic().




On Wednesday, 7 March 2018 16:05:49 UTC-5, Mike Bayer wrote:
>
> my test doesn't have that problem, try calling configure_mappers() 
> first.   Try running the test case I have as given. 
>
> On Wed, Mar 7, 2018 at 4:02 PM, Harshvardhan Gupta 
> <harsh...@gmail.com > wrote: 
> > Thanks for the reply. 
> > 
> > Sorry, I will be more careful when i sent code snippets. 
> > 
> > I tried this out after you asked me to remove flat =True, so the code 
> looks 
> > like this now : 
> > 
> > 
> > pjoin = polymorphic_union({ 
> > 'order_test_item': OrderTestItem.__table__, 
> > }, 'type', 'pjoin')   # I dont think 'type' should exist. 
> > 
> > 
> > order_items = with_polymorphic( 
> > OrderItem, [OrderTestItem], selectable=pjoin) 
> > 
> > 
> > Now i get a different Error : 
> > 
> > 
> --- 
> > UnmappedClassErrorTraceback (most recent call 
> last) 
> >  in () 
> >   4 
> >   5 order_items = with_polymorphic( 
> > > 6 OrderItem, [OrderTestItem], selectable=pjoin) 
> > 
> > 
> ~/.virtualenvs/app-flask-rest-backend-36/lib/python3.6/site-packages/sqlalchemy/orm/util.py
>  
>
> > in with_polymorphic(base, classes, selectable, flat, polymorphic_on, 
> > aliased, innerjoin, _use_mapper_path, _existing_alias) 
> > 771only be specified if querying for one specific subtype 
> only 
> > 772 """ 
> > --> 773 primary_mapper = _class_to_mapper(base) 
> > 774 if _existing_alias: 
> > 775 assert _existing_alias.mapper is primary_mapper 
> > 
> > 
> ~/.virtualenvs/app-flask-rest-backend-36/lib/python3.6/site-packages/sqlalchemy/orm/base.py
>  
>
> > in _class_to_mapper(class_or_mapper) 
> > 308 return insp.mapper 
> > 309 else: 
> > --> 310 raise exc.UnmappedClassError(class_or_mapper) 
> > 311 
> > 312 
> > 
> > UnmappedClassError: Class 'models.Item.OrderItem' is not mapped 
> > 
> > 
> > My models are defined as my original post, like this : 
> > 
> > 
> > class OrderItem(Dictifiable, AbstractConcreteBase, db.Model): 
> >  pass 
> > 
> > 
> > class OrderTestItem(OrderItem): 
> >  order_id = Column(Integer, ForeignKey("order.id"), primary_key=True) 
> >  test_id = Column(Integer, ForeignKey("test.id"), primary_key=True) 
> > 
> >  test = relationship('Test') 
> >  order = relationship('Order') 
> > 
> >  __mapper_args__ = { 
> >  'polymorphic_identity': 'order_test_item', 
> >  'concrete': True, 
> >  } 
> > 
> > 
> > class Order(Dictifiable, db.Model): 
> > id = Column(Integer, Sequence('user_id_seq'), primary_key=True) 
> > rp_id = Column(Integer, nullable=False) 
> > 
> > user_id = Column(Integer, ForeignKey('user.id')) 
> > 
> > user = relationship('User') 
> > items = relationship('OrderItem') 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > On Wednesday, 7 March 2018 15:48:58 UTC-5, Mike Bayer wrote: 
> >> 
> >> here's a full MCVE, which with "flat=True" reproduces your issue 
> >> exactly (send me this next time).   The issue is the "flat=True", take 
> >> that out because it isn't compatible with a concrete union - it 
> >> implies the "aliased" flag and that gets in the way of what the 
> >> polymorphic_union() function is doing already. 
> >> 
> >> 
> >> from sqlalchemy import * 
> >> from sqlalchemy.orm import * 
> >> from sqlalchemy.ext.declarative import declarative_base 
> >> from sqlalchemy.ext.declarative import declared_attr 

Re: [sqlalchemy] Polymorphic AbstractConcreteBase , eager load , conditions , etc

2018-03-07 Thread Harshvardhan Gupta
Thanks for the reply. 

Sorry, I will be more careful when i sent code snippets. 

I tried this out after you asked me to remove flat =True, so the code looks 
like this now : 


pjoin = polymorphic_union({ 
'order_test_item': OrderTestItem.__table__, 
}, 'type', 'pjoin')   # I dont think 'type' should exist.


order_items = with_polymorphic( 
OrderItem, [OrderTestItem], selectable=pjoin) 


Now i get a different Error : 

---UnmappedClassError
Traceback (most recent call 
last) in ()  4   5 order_items = 
with_polymorphic( > 6 OrderItem, [OrderTestItem], selectable=pjoin) 
~/.virtualenvs/app-flask-rest-backend-36/lib/python3.6/site-packages/sqlalchemy/orm/util.py
 in with_polymorphic(base, classes, selectable, flat, polymorphic_on, aliased, 
innerjoin, _use_mapper_path, _existing_alias)771only be specified 
if querying for one specific subtype only772 """--> 773 
primary_mapper = _class_to_mapper(base)774 if _existing_alias:775   
  assert _existing_alias.mapper is primary_mapper
~/.virtualenvs/app-flask-rest-backend-36/lib/python3.6/site-packages/sqlalchemy/orm/base.py
 in _class_to_mapper(class_or_mapper)308 return insp.mapper309  
   else:--> 310 raise exc.UnmappedClassError(class_or_mapper)311
 312 
UnmappedClassError: Class 'models.Item.OrderItem' is not mapped


My models are defined as my original post, like this : 


class OrderItem(Dictifiable, AbstractConcreteBase, db.Model):
 pass


class OrderTestItem(OrderItem):
 order_id = Column(Integer, ForeignKey("order.id"), primary_key=True)
 test_id = Column(Integer, ForeignKey("test.id"), primary_key=True)

 test = relationship('Test')
 order = relationship('Order')

 __mapper_args__ = {
 'polymorphic_identity': 'order_test_item',
 'concrete': True,
 }


class Order(Dictifiable, db.Model):
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
rp_id = Column(Integer, nullable=False)

user_id = Column(Integer, ForeignKey('user.id'))

user = relationship('User')
items = relationship('OrderItem')







On Wednesday, 7 March 2018 15:48:58 UTC-5, Mike Bayer wrote:
>
> here's a full MCVE, which with "flat=True" reproduces your issue 
> exactly (send me this next time).   The issue is the "flat=True", take 
> that out because it isn't compatible with a concrete union - it 
> implies the "aliased" flag and that gets in the way of what the 
> polymorphic_union() function is doing already. 
>
>
> from sqlalchemy import * 
> from sqlalchemy.orm import * 
> from sqlalchemy.ext.declarative import declarative_base 
> from sqlalchemy.ext.declarative import declared_attr 
> from sqlalchemy.ext.declarative import AbstractConcreteBase 
>
> Base = declarative_base() 
>
>
> class OrderItem(AbstractConcreteBase, Base): 
> pass 
>
>
> class OrderTestItem(OrderItem): 
> __tablename__ = 'order_test_item' 
>
> order_id = Column(Integer, ForeignKey("order.id"), primary_key=True) 
> test_id = Column(Integer, ForeignKey("test.id"), primary_key=True) 
>
> test = relationship('Test') 
> order = relationship('Order') 
>
> __mapper_args__ = { 
> 'polymorphic_identity': 'order_test_item', 
> 'concrete': True 
> } 
>
>
> class User(Base): 
> __tablename__ = 'user' 
> id = Column(Integer, primary_key=True) 
>
>
> class Test(Base): 
> __tablename__ = 'test' 
> id = Column(Integer, primary_key=True) 
>
>
> class Order(Base): 
> __tablename__ = 'order' 
>
> id = Column(Integer, primary_key=True) 
>
> user_id = Column(Integer, ForeignKey('user.id')) 
>
> user = relationship('User') 
> items = relationship('OrderItem') 
>
> e = create_engine("sqlite://", echo=True) 
> Base.metadata.create_all(e) 
>
> s = Session(e) 
>
> t1, t2 = Test(), Test() 
> s.add(Order(items=[OrderTestItem(test=t1), OrderTestItem(test=t2)])) 
> s.commit() 
>
>
> pjoin = polymorphic_union({ 
>     'order_test_item': OrderTestItem.__table__, 
> }, 'type', 'pjoin') 
>
> order_items = with_polymorphic( 
> OrderItem, [OrderTestItem], selectable=pjoin) 
>
>
> s = Session(e) 
> order = s.query(Order).join(Order.items.of_type(order_items)).all() 
>
>
>
>
> On Wed, Mar 7, 2018 at 3:36 PM, Harshvardhan Gupta 
> <harsh...@gmail.com > wrote: 
> > Thanks for replying. 
> > I forgot to show the error. 
> > It is this: 
> > 
> > 1054, "Unknown column 'pjoin.order_id' in 'on clause'") [SQL: "SELECT 
> > `order`.id AS order_id, `o

Re: [sqlalchemy] Polymorphic AbstractConcreteBase , eager load , conditions , etc

2018-03-07 Thread Harshvardhan Gupta
Thanks for replying. 
I forgot to show the error. 
It is this: 

1054, "Unknown column 'pjoin.order_id' in 'on clause'") [SQL: "SELECT 
`order`.id AS order_id, `order`.rp_id AS order_rp_id, `order`.user_id AS 
order_user_id \nFROM `order` INNER JOIN (SELECT order_test_item.order_id AS 
order_id, order_test_item.test_id AS test_id, 'order_test_item' AS type \nFROM 
order_test_item) AS anon_1 ON `order`.id = pjoin.order_id"




On Wednesday, 7 March 2018 15:28:54 UTC-5, Mike Bayer wrote:
>
> On Tue, Mar 6, 2018 at 8:36 PM, Harshvardhan Gupta 
> <harsh...@gmail.com > wrote: 
> > I tried to use AbstractConcreteBase for polymorphic relationships , but 
> I am 
> > getting errors. The examples in sqlalchemy cover normal polymorphism 
> well, 
> > but not those with Abstract Base classes. 
> > 
> > 
> > I have already asked a question on stack overflow . 
> > 
> > The gist of the question is: 
> > 
> > class OrderItem(Dictifiable, AbstractConcreteBase, db.Model): 
> > pass 
> > 
> > 
> > class OrderTestItem(OrderItem): 
> > order_id = Column(Integer, ForeignKey("order.id"), 
> primary_key=True) 
> > test_id = Column(Integer, ForeignKey("test.id"), primary_key=True) 
> > 
> > test = relationship('Test') 
> > order = relationship('Order') 
> > 
> > __mapper_args__ = { 
> > 'polymorphic_identity': 'order_test_item', 
> > 'concrete': True 
> > } 
> > 
> > 
> > 
> > class Order(Dictifiable, db.Model): # This class has a relation to the 
> > polymorphic class 
> > 
> > id = Column(Integer, Sequence('user_id_seq'), primary_key=True) 
> > 
> > user_id = Column(Integer, ForeignKey('user.id')) 
> > 
> > user = relationship('User') 
> > items = relationship('OrderItem') 
> > 
> > 
> > I query like : 
> > 
> > pjoin = polymorphic_union({ 
> > 'order_test_item': OrderTestItem.__table__, 
> > }, 'type', 'pjoin') 
> > 
> > order_items = 
> > with_polymorphic(OrderItem,[OrderTestItem],selectable=pjoin,flat=True) 
> > 
> > And my actual query : 
> > 
> > order = Order.query.join(Order.items.of_type(order_items)).all() 
> > 
> > 
> > I would like to know what the correct way to query these tables is, how 
> to 
> > eager load polymorphic tables, and how to filter on the relationships. 
>
> looks correct to me.  what is "the error" ?   can you provide full 
> MCVE + complete stack trace? 
>
>
>
> > 
> > I plan to send a pull request with an example of these test cases after 
> I 
> > know the answers myself. 
> > 
> > 
> > -- 
> > 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 post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

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


[sqlalchemy] Polymorphic AbstractConcreteBase , eager load , conditions , etc

2018-03-06 Thread Harshvardhan Gupta
I tried to use AbstractConcreteBase for polymorphic relationships , but I 
am getting errors. The examples in sqlalchemy cover normal polymorphism 
well, but not those with Abstract Base classes. 


I have already asked a question on stack overflow  

. 

The gist of the question is: 

class OrderItem(Dictifiable, AbstractConcreteBase, db.Model):
pass


class OrderTestItem(OrderItem):
order_id = Column(Integer, ForeignKey("order.id"), primary_key=True)
test_id = Column(Integer, ForeignKey("test.id"), primary_key=True)

test = relationship('Test')
order = relationship('Order')

__mapper_args__ = {
'polymorphic_identity': 'order_test_item',
'concrete': True
}



class Order(Dictifiable, db.Model): # This class has a relation to the 
polymorphic class


id = Column(Integer, Sequence('user_id_seq'), primary_key=True)

user_id = Column(Integer, ForeignKey('user.id'))

user = relationship('User')
items = relationship('OrderItem')


I query like : 

pjoin = polymorphic_union({
'order_test_item': OrderTestItem.__table__,
}, 'type', 'pjoin')

order_items = 
with_polymorphic(OrderItem,[OrderTestItem],selectable=pjoin,flat=True)

And my actual query : 

order = Order.query.join(Order.items.of_type(order_items)).all()


I would like to know what the correct way to query these tables is, how to 
eager load polymorphic tables, and how to filter on the relationships. 

I plan to send a pull request with an example of these test cases after I know 
the answers myself. 


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


Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
Thanks! 
make_transient_to_detached did the trick! 

ill add the answer to my question. 

On Friday, 2 March 2018 11:56:30 UTC-5, Mike Bayer wrote:
>
> please add an answer to your stackoverflow question as well 
>
> On Fri, Mar 2, 2018 at 11:55 AM, Mike Bayer <mik...@zzzcomputing.com 
> > wrote: 
> > On Fri, Mar 2, 2018 at 11:55 AM, Harshvardhan Gupta 
> > <harsh...@gmail.com > wrote: 
> >> I meant, do the query only when email is required, and not unless 
> anything 
> >> else is required. 
> >> if I call merge(), the query is instantly emitted. I want it to be 
> emitted 
> >> only if an unloaded attribute is requested. 
> > 
> > make the object act like detached and expired, then associate with the 
> session: 
> > 
> > from sqlalchemy.orm.session import make_transient_to_detached 
> > a1 = A(id=1) 
> > make_transient_to_detached(a1) 
> > s.add(a1) 
> > 
> > print("---") 
> > assert a1.data == "some data" 
> > assert len(a1.bs) == 3 
> > 
> > 
> > 
> > 
> > 
> >> 
> >> On Friday, 2 March 2018 11:51:20 UTC-5, Mike Bayer wrote: 
> >>> 
> >>> On Fri, Mar 2, 2018 at 11:30 AM, Harshvardhan Gupta 
> >>> <harsh...@gmail.com> wrote: 
> >>> > There is no way to achieve what I want without doing the query? 
> >>> > merge seems to do the whole query. 
> >>> 
> >>> you have a User row, and you want to load columns in that row, like 
> >>> "full_name".How would you like to achieve that without emitting a 
> >>> query? 
> >>> 
> >>> 
> >>> 
> >>> > I also have asked this question on stack overflow which talks about 
> my 
> >>> > use 
> >>> > case : 
> >>> > 
> >>> > 
> >>> > 
> https://stackoverflow.com/questions/49062520/querying-properties-of-transient-models-in-sqlalchemy
>  
> >>> > 
> >>> > 
> >>> > On Friday, 2 March 2018 11:27:31 UTC-5, Mike Bayer wrote: 
> >>> >> 
> >>> >> On Fri, Mar 2, 2018 at 11:21 AM, Harshvardhan Gupta 
> >>> >> <harsh...@gmail.com> wrote: 
> >>> >> > I also tried loading a related attribute , and it still returns 
> None. 
> >>> >> 
> >>> >> 
> >>> >> you need to merge the object: 
> >>> >> 
> >>> >> my_user = session.merge(my_user) 
> >>> >> 
> >>> >> that is your only option.  E.g. do the query. 
> >>> >> 
> >>> >> > 
> >>> >> > 
> >>> >> > On Friday, 2 March 2018 11:00:47 UTC-5, Harshvardhan Gupta wrote: 
> >>> >> >> 
> >>> >> >> Thanks, I am able to call my object, but the attribute prints 
> none: 
> >>> >> >> 
> >>> >> >> 
> >>> >> >> E.g. my model is: 
> >>> >> >> 
> >>> >> >> class User(Dictifiable, db.Model, UserMixin): 
> >>> >> >> __tablename__ = 'user' 
> >>> >> >> 
> >>> >> >> id = Column(Integer, Sequence('user_id_seq'), 
> primary_key=True) 
> >>> >> >> full_name = Column(String(50)) 
> >>> >> >> 
> >>> >> >> 
> >>> >> >> And this is what I have done : 
> >>> >> >> 
> >>> >> >> `u_lazy = User(id=1)` 
> >>> >> >> `db.session().enable_relationship_loading(u_lazy)` 
> >>> >> >> when i try to print u_lazy.full_name, it shows null (it is non 
> null 
> >>> >> >> in 
> >>> >> >> db 
> >>> >> >> ) 
> >>> >> >> 
> >>> >> >> 
> >>> >> >> On Friday, 2 March 2018 10:55:11 UTC-5, Mike Bayer wrote: 
> >>> >> >>> 
> >>> >> >>> On Fri, Mar 2, 2018 at 10:52 AM, Harshvardhan Gupta 
> >>> >> >>> <harsh...@gmail.com> wrote: 
> >>> >> >>> > Thanks for the reply. 
> >>> >> >>> > 
> >>> >> >>> > There is a guarantee that the user will exist ,why is why I 
> want 
> >>> >> >>&g

Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
I meant, do the query only when email is required, and not unless anything 
else is required. 
if I call merge(), the query is instantly emitted. I want it to be emitted 
only if an unloaded attribute is requested. 

On Friday, 2 March 2018 11:51:20 UTC-5, Mike Bayer wrote:
>
> On Fri, Mar 2, 2018 at 11:30 AM, Harshvardhan Gupta 
> <harsh...@gmail.com > wrote: 
> > There is no way to achieve what I want without doing the query? 
> > merge seems to do the whole query. 
>
> you have a User row, and you want to load columns in that row, like 
> "full_name".How would you like to achieve that without emitting a 
> query? 
>
>
>
> > I also have asked this question on stack overflow which talks about my 
> use 
> > case : 
> > 
> > 
> https://stackoverflow.com/questions/49062520/querying-properties-of-transient-models-in-sqlalchemy
>  
> > 
> > 
> > On Friday, 2 March 2018 11:27:31 UTC-5, Mike Bayer wrote: 
> >> 
> >> On Fri, Mar 2, 2018 at 11:21 AM, Harshvardhan Gupta 
> >> <harsh...@gmail.com> wrote: 
> >> > I also tried loading a related attribute , and it still returns None. 
> >> 
> >> 
> >> you need to merge the object: 
> >> 
> >> my_user = session.merge(my_user) 
> >> 
> >> that is your only option.  E.g. do the query. 
> >> 
> >> > 
> >> > 
> >> > On Friday, 2 March 2018 11:00:47 UTC-5, Harshvardhan Gupta wrote: 
> >> >> 
> >> >> Thanks, I am able to call my object, but the attribute prints none: 
> >> >> 
> >> >> 
> >> >> E.g. my model is: 
> >> >> 
> >> >> class User(Dictifiable, db.Model, UserMixin): 
> >> >> __tablename__ = 'user' 
> >> >> 
> >> >> id = Column(Integer, Sequence('user_id_seq'), primary_key=True) 
> >> >> full_name = Column(String(50)) 
> >> >> 
> >> >> 
> >> >> And this is what I have done : 
> >> >> 
> >> >> `u_lazy = User(id=1)` 
> >> >> `db.session().enable_relationship_loading(u_lazy)` 
> >> >> when i try to print u_lazy.full_name, it shows null (it is non null 
> in 
> >> >> db 
> >> >> ) 
> >> >> 
> >> >> 
> >> >> On Friday, 2 March 2018 10:55:11 UTC-5, Mike Bayer wrote: 
> >> >>> 
> >> >>> On Fri, Mar 2, 2018 at 10:52 AM, Harshvardhan Gupta 
> >> >>> <harsh...@gmail.com> wrote: 
> >> >>> > Thanks for the reply. 
> >> >>> > 
> >> >>> > There is a guarantee that the user will exist ,why is why I want 
> to 
> >> >>> > prevent that extra db query. 
> >> >>> > 
> >> >>> > When I try to do enable relationship loading ,I get the error : 
> >> >>> > Scoped session has no attribute enable relationship loading. 
> >> >>> 
> >> >>> that's a scoped_session(), not a Session, access the Session by 
> >> >>> calling it:  scoped_session().enable_relationship_loading(obj) 
> >> >>> 
> >> >>> > 
> >> >>> > -- 
> >> >>> > 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 post to this group, send email to sqlal...@googlegroups.com. 
> >> >>> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> >> >>> > For more options, visit https://groups.google.com/d/optout. 
> >> > 
> >> > -- 
> >> > SQLAlchemy - 
> >> >

Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
There is no way to achieve what I want without doing the query? 
merge seems to do the whole query. 
I also have asked this question on stack overflow which talks about my use 
case : 

https://stackoverflow.com/questions/49062520/querying-properties-of-transient-models-in-sqlalchemy


On Friday, 2 March 2018 11:27:31 UTC-5, Mike Bayer wrote:
>
> On Fri, Mar 2, 2018 at 11:21 AM, Harshvardhan Gupta 
> <harsh...@gmail.com > wrote: 
> > I also tried loading a related attribute , and it still returns None. 
>
>
> you need to merge the object: 
>
> my_user = session.merge(my_user) 
>
> that is your only option.  E.g. do the query. 
>
> > 
> > 
> > On Friday, 2 March 2018 11:00:47 UTC-5, Harshvardhan Gupta wrote: 
> >> 
> >> Thanks, I am able to call my object, but the attribute prints none: 
> >> 
> >> 
> >> E.g. my model is: 
> >> 
> >> class User(Dictifiable, db.Model, UserMixin): 
> >> __tablename__ = 'user' 
> >> 
> >> id = Column(Integer, Sequence('user_id_seq'), primary_key=True) 
> >> full_name = Column(String(50)) 
> >> 
> >> 
> >> And this is what I have done : 
> >> 
> >> `u_lazy = User(id=1)` 
> >> `db.session().enable_relationship_loading(u_lazy)` 
> >> when i try to print u_lazy.full_name, it shows null (it is non null in 
> db 
> >> ) 
> >> 
> >> 
> >> On Friday, 2 March 2018 10:55:11 UTC-5, Mike Bayer wrote: 
> >>> 
> >>> On Fri, Mar 2, 2018 at 10:52 AM, Harshvardhan Gupta 
> >>> <harsh...@gmail.com> wrote: 
> >>> > Thanks for the reply. 
> >>> > 
> >>> > There is a guarantee that the user will exist ,why is why I want to 
> >>> > prevent that extra db query. 
> >>> > 
> >>> > When I try to do enable relationship loading ,I get the error : 
> >>> > Scoped session has no attribute enable relationship loading. 
> >>> 
> >>> that's a scoped_session(), not a Session, access the Session by 
> >>> calling it:  scoped_session().enable_relationship_loading(obj) 
> >>> 
> >>> > 
> >>> > -- 
> >>> > 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 post to this group, send email to sqlal...@googlegroups.com. 
> >>> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> >>> > For more options, visit https://groups.google.com/d/optout. 
> > 
> > -- 
> > 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 post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

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


Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
I also tried loading a related attribute , and it still returns None. 

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


Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
I also tried loading a related attribute , and it still returns None. 

On Friday, 2 March 2018 11:00:47 UTC-5, Harshvardhan Gupta wrote:
>
> Thanks, I am able to call my object, but the attribute prints none: 
>
>
> E.g. my model is: 
>
> class User(Dictifiable, db.Model, UserMixin):
> __tablename__ = 'user'
>
> id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
> full_name = Column(String(50))
>
>
> And this is what I have done : 
>
> `u_lazy = User(id=1)`
> `db.session().enable_relationship_loading(u_lazy)`
> when i try to print u_lazy.full_name, it shows null (it is non null in db 
>  ) 
>
>
> On Friday, 2 March 2018 10:55:11 UTC-5, Mike Bayer wrote:
>>
>> On Fri, Mar 2, 2018 at 10:52 AM, Harshvardhan Gupta 
>> <harsh...@gmail.com> wrote: 
>> > Thanks for the reply. 
>> > 
>> > There is a guarantee that the user will exist ,why is why I want to 
>> prevent that extra db query. 
>> > 
>> > When I try to do enable relationship loading ,I get the error : 
>> > Scoped session has no attribute enable relationship loading. 
>>
>> that's a scoped_session(), not a Session, access the Session by 
>> calling it:  scoped_session().enable_relationship_loading(obj) 
>>
>> > 
>> > -- 
>> > 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 post to this group, send email to sqlal...@googlegroups.com. 
>> > Visit this group at https://groups.google.com/group/sqlalchemy. 
>> > For more options, visit https://groups.google.com/d/optout. 
>>
>

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


Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
Thanks, I am able to call my object, but the attribute prints none: 


E.g. my model is: 

class User(Dictifiable, db.Model, UserMixin):
__tablename__ = 'user'

id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
full_name = Column(String(50))


And this is what I have done : 

`u_lazy = User(id=1)`
`db.session().enable_relationship_loading(u_lazy)`
when i try to print u_lazy.full_name, it shows null (it is non null in db 
 ) 


On Friday, 2 March 2018 10:55:11 UTC-5, Mike Bayer wrote:
>
> On Fri, Mar 2, 2018 at 10:52 AM, Harshvardhan Gupta 
> <harsh...@gmail.com > wrote: 
> > Thanks for the reply. 
> > 
> > There is a guarantee that the user will exist ,why is why I want to 
> prevent that extra db query. 
> > 
> > When I try to do enable relationship loading ,I get the error : 
> > Scoped session has no attribute enable relationship loading. 
>
> that's a scoped_session(), not a Session, access the Session by 
> calling it:  scoped_session().enable_relationship_loading(obj) 
>
> > 
> > -- 
> > 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 post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

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


Re: [sqlalchemy] Load columns/relationships of transient objects

2018-03-02 Thread Harshvardhan Gupta
Thanks for the reply. 

There is a guarantee that the user will exist ,why is why I want to prevent 
that extra db query.

When I try to do enable relationship loading ,I get the error : 
Scoped session has no attribute enable relationship loading. 

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