Re: [sqlalchemy] SqlAlchemy dynamic query generation
Hi Robert Forkel, You got it right i am trying to have a combination of expression combined in and_. I need to add the attribute only when it is not equal to null.So i tried to check if it is not null and then from the expression and pass it to filter .But this does not work. I went through the document but unable to figure out how to do this through sql. Thank you On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel xrotw...@googlemail.comwrote: You may want to look at the tutorial [1]. In the code you pasted you are assembling a string to pass to filter. While you can do that (but then it should be proper sql not python!), what you want to do is passing a python expression like db.User.age==age, or a combination of expressions combined using and_, or_, etc. [1] http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote: I am getting the following error ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 1,db.User.age==23,d' at line 3) Is there any way by which i can generate dynamic query? Thank you Sana On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron cameron.jack...@thalesgroup.com.au wrote: You might get more help if you provide more details. What error are you getting? -Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On Behalf Of Sana Sent: Thursday, 22 December 2011 2:50 AM To: sqlalchemy Subject: [sqlalchemy] SqlAlchemy dynamic query generation Hi All, I am trying to do query based on the user input as follows condition = 'and_(db.User.id == id' if user_name != ' ': condition += ',db.User.name == user_name' elif age != ' ': condition += ',db.User.age == age' elif place != ' ': condition += ',db.User.place == place' where = condition+')' result = db.User.filter(where).all() But Im getting error when i do this. Is there any way by which i can do this Thank you Sana -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. - DISCLAIMER: This e-mail transmission and any documents, files and previous e-mail messages attached to it are private and confidential. They may contain proprietary or copyright material or information that is subject to legal professional privilege. They are for the use of the intended recipient only. Any unauthorised viewing, use, disclosure, copying, alteration, storage or distribution of, or reliance on, this message is strictly prohibited. No part may be reproduced, adapted or transmitted without the written permission of the owner. If you have received this transmission in error, or are not an authorised recipient, please immediately notify the sender by return email, delete this message and all copies from your e-mail system, and destroy any printed copies. Receipt by anyone other than the intended recipient should not be deemed a waiver of any privilege or protection. Thales Australia does not warrant or represent that this e-mail or any documents, files and previous e-mail messages attached are error or virus free. - -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to
Re: [sqlalchemy] SqlAlchemy dynamic query generation
conditions = [db.User.id==id] if user_name != ' ': conditions.append(db.User.name == user_name) elif age != ' ': conditions.append(db.User.age == age) elif place != ' ': conditions.append(db.User.place == place) result = db.User.filter(and_(*conditions)).all() might do the trick (untested). On Thu, Dec 22, 2011 at 9:53 AM, Sana klh sana.kl...@gmail.com wrote: Hi Robert Forkel, You got it right i am trying to have a combination of expression combined in and_. I need to add the attribute only when it is not equal to null.So i tried to check if it is not null and then from the expression and pass it to filter .But this does not work. I went through the document but unable to figure out how to do this through sql. Thank you On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel xrotw...@googlemail.com wrote: You may want to look at the tutorial [1]. In the code you pasted you are assembling a string to pass to filter. While you can do that (but then it should be proper sql not python!), what you want to do is passing a python expression like db.User.age==age, or a combination of expressions combined using and_, or_, etc. [1] http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote: I am getting the following error ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 1,db.User.age==23,d' at line 3) Is there any way by which i can generate dynamic query? Thank you Sana On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron cameron.jack...@thalesgroup.com.au wrote: You might get more help if you provide more details. What error are you getting? -Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On Behalf Of Sana Sent: Thursday, 22 December 2011 2:50 AM To: sqlalchemy Subject: [sqlalchemy] SqlAlchemy dynamic query generation Hi All, I am trying to do query based on the user input as follows condition = 'and_(db.User.id == id' if user_name != ' ': condition += ',db.User.name == user_name' elif age != ' ': condition += ',db.User.age == age' elif place != ' ': condition += ',db.User.place == place' where = condition+')' result = db.User.filter(where).all() But Im getting error when i do this. Is there any way by which i can do this Thank you Sana -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. - DISCLAIMER: This e-mail transmission and any documents, files and previous e-mail messages attached to it are private and confidential. They may contain proprietary or copyright material or information that is subject to legal professional privilege. They are for the use of the intended recipient only. Any unauthorised viewing, use, disclosure, copying, alteration, storage or distribution of, or reliance on, this message is strictly prohibited. No part may be reproduced, adapted or transmitted without the written permission of the owner. If you have received this transmission in error, or are not an authorised recipient, please immediately notify the sender by return email, delete this message and all copies from your e-mail system, and destroy any printed copies. Receipt by anyone other than the intended recipient should not be deemed a waiver of any privilege or protection. Thales Australia does not warrant or represent that this e-mail or any documents, files and previous e-mail messages attached are error or virus free. - -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To
Re: [sqlalchemy] SqlAlchemy dynamic query generation
I tried it but i still get the same error. On Thu, Dec 22, 2011 at 2:31 PM, Robert Forkel xrotw...@googlemail.comwrote: conditions = [db.User.id==id] if user_name != ' ': conditions.append(db.User.name == user_name) elif age != ' ': conditions.append(db.User.age == age) elif place != ' ': conditions.append(db.User.place == place) result = db.User.filter(and_(*conditions)).all() might do the trick (untested). On Thu, Dec 22, 2011 at 9:53 AM, Sana klh sana.kl...@gmail.com wrote: Hi Robert Forkel, You got it right i am trying to have a combination of expression combined in and_. I need to add the attribute only when it is not equal to null.So i tried to check if it is not null and then from the expression and pass it to filter .But this does not work. I went through the document but unable to figure out how to do this through sql. Thank you On Thu, Dec 22, 2011 at 12:11 PM, Robert Forkel xrotw...@googlemail.com wrote: You may want to look at the tutorial [1]. In the code you pasted you are assembling a string to pass to filter. While you can do that (but then it should be proper sql not python!), what you want to do is passing a python expression like db.User.age==age, or a combination of expressions combined using and_, or_, etc. [1] http://www.sqlalchemy.org/docs/orm/tutorial.html#common-filter-operators On Thu, Dec 22, 2011 at 6:34 AM, Sana klh sana.kl...@gmail.com wrote: I am getting the following error ProgrammingError: (ProgrammingError) (1064, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 1,db.User.age==23,d' at line 3) Is there any way by which i can generate dynamic query? Thank you Sana On Thu, Dec 22, 2011 at 4:40 AM, Jackson, Cameron cameron.jack...@thalesgroup.com.au wrote: You might get more help if you provide more details. What error are you getting? -Original Message- From: sqlalchemy@googlegroups.com [mailto: sqlalchemy@googlegroups.com] On Behalf Of Sana Sent: Thursday, 22 December 2011 2:50 AM To: sqlalchemy Subject: [sqlalchemy] SqlAlchemy dynamic query generation Hi All, I am trying to do query based on the user input as follows condition = 'and_(db.User.id == id' if user_name != ' ': condition += ',db.User.name == user_name' elif age != ' ': condition += ',db.User.age == age' elif place != ' ': condition += ',db.User.place == place' where = condition+')' result = db.User.filter(where).all() But Im getting error when i do this. Is there any way by which i can do this Thank you Sana -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. - DISCLAIMER: This e-mail transmission and any documents, files and previous e-mail messages attached to it are private and confidential. They may contain proprietary or copyright material or information that is subject to legal professional privilege. They are for the use of the intended recipient only. Any unauthorised viewing, use, disclosure, copying, alteration, storage or distribution of, or reliance on, this message is strictly prohibited. No part may be reproduced, adapted or transmitted without the written permission of the owner. If you have received this transmission in error, or are not an authorised recipient, please immediately notify the sender by return email, delete this message and all copies from your e-mail system, and destroy any printed copies. Receipt by anyone other than the intended recipient should not be deemed a waiver of any privilege or protection. Thales Australia does not warrant or represent that this e-mail or any documents, files and previous e-mail messages attached are error or virus free. - -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com.
[sqlalchemy] declarative and late reflection?
Hey list, this sounds like it should be a FAQ, didn't find anything though: I want to use the ORM in a declarative style and have the table definition reflected, eg sth like: class A(declarative_base()): __tablename__ = 'A' __table_args__ = {'autoload' : True} However to do this SA (quite reasonably, really) already needs an engine. For various reasons I can only construct one after import time however. Is there a way to do a kind of late reflection, ie. have the above class definition but trigger the reflection part later? I could of course generate the class object later in a function when the engine is already available but maybe there's something more elegant... Many thanks, peter. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] declarative and late reflection?
On Dec 22, 2011, at 9:37 AM, peter sabaini wrote: Hey list, this sounds like it should be a FAQ, didn't find anything though: I want to use the ORM in a declarative style and have the table definition reflected, eg sth like: class A(declarative_base()): __tablename__ = 'A' __table_args__ = {'autoload' : True} However to do this SA (quite reasonably, really) already needs an engine. For various reasons I can only construct one after import time however. Is there a way to do a kind of late reflection, ie. have the above class definition but trigger the reflection part later? I could of course generate the class object later in a function when the engine is already available but maybe there's something more elegant... I was about to type up this recipe on the wiki and then most awesomely I already did it for someone ! hooray. The current technique for this is at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeReflectedBase . -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] declarative and late reflection?
Hey! This works for me -- almost :-) In my use case I need to override a column (to provide an artificial FK -- some *erm old school mysql db) which seems to trigger SA into trying to reflect early Observe: from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base, declared_attr class DeclarativeReflectedBase(object): _mapper_args = [] @classmethod def __mapper_cls__(cls, *args, **kw): Declarative will use this function in lieu of calling mapper() directly. Collect each series of arguments and invoke them when prepare() is called. cls._mapper_args.append((args, kw)) @classmethod def prepare(cls, engine): Reflect all the tables and map ! for args, kw in cls._mapper_args: klass = args[0] klass.__table__ = table = Table( klass.__tablename__, cls.metadata, autoload=True, autoload_with=engine) klass.__mapper__ = mapper(klass, table, **kw) @declared_attr def __table__(cls): Return a placeholder to lull declarative into complacency return object() Base = declarative_base(cls=DeclarativeReflectedBase) class Foo(Base): __tablename__ = 'foo' quux = Column(String) bars = relationship(Bar) class Bar(Base): __tablename__ = 'bar' Gives: Traceback (most recent call last): File stdin, line 1, in module File /usr/tmp/Python6244Mux.py, line 40, in module class Foo(Base): File /usr/lib/python2.6/site-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/ext/declarative.py, line 1273, in __init__ _as_declarative(cls, classname, cls.__dict__) File /usr/lib/python2.6/site-packages/SQLAlchemy-0.7.4-py2.6-linux-x86_64.egg/sqlalchemy/ext/declarative.py, line 1177, in _as_declarative if not table.c.contains_column(c): AttributeError: 'object' object has no attribute 'c' Hm, maybe I can try to add the column override later? Thanks again peter. On Thu, Dec 22, 2011 at 4:41 PM, Michael Bayer mike...@zzzcomputing.comwrote: On Dec 22, 2011, at 9:37 AM, peter sabaini wrote: Hey list, this sounds like it should be a FAQ, didn't find anything though: I want to use the ORM in a declarative style and have the table definition reflected, eg sth like: class A(declarative_base()): __tablename__ = 'A' __table_args__ = {'autoload' : True} However to do this SA (quite reasonably, really) already needs an engine. For various reasons I can only construct one after import time however. Is there a way to do a kind of late reflection, ie. have the above class definition but trigger the reflection part later? I could of course generate the class object later in a function when the engine is already available but maybe there's something more elegant... I was about to type up this recipe on the wiki and then most awesomely I already did it for someone ! hooray. The current technique for this is at http://www.sqlalchemy.org/trac/wiki/UsageRecipes/DeclarativeReflectedBase. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- http://sabaini.at -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] declarative and late reflection?
On Dec 22, 2011, at 11:48 AM, peter sabaini wrote: Hey! This works for me -- almost :-) In my use case I need to override a column (to provide an artificial FK -- some *erm old school mysql db) which seems to trigger SA into trying to reflect early Observe: from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base, declared_attr class DeclarativeReflectedBase(object): _mapper_args = [] @classmethod def __mapper_cls__(cls, *args, **kw): Declarative will use this function in lieu of calling mapper() directly. Collect each series of arguments and invoke them when prepare() is called. cls._mapper_args.append((args, kw)) @classmethod def prepare(cls, engine): Reflect all the tables and map ! for args, kw in cls._mapper_args: klass = args[0] klass.__table__ = table = Table( klass.__tablename__, cls.metadata, autoload=True, autoload_with=engine) klass.__mapper__ = mapper(klass, table, **kw) @declared_attr def __table__(cls): Return a placeholder to lull declarative into complacency return object() Base = declarative_base(cls=DeclarativeReflectedBase) class Foo(Base): __tablename__ = 'foo' quux = Column(String) bars = relationship(Bar) class Bar(Base): __tablename__ = 'bar' yah well you want to not declare Column objects on the declared class directly in this case because that trips off declarative into building up a Table, which you don't have here, it only returns object. this could work really nicely with extend_existing, which has been enhanced in 0.7.4, but there seem to be some glitches preventing it from being super nice, so I can't get that to work right now. Just send in those columns via your own means: class Base(object): # ... extra_cols = [] @classmethod def prepare(cls, engine): Reflect all the tables and map ! for args, kw in cls._mapper_args: klass = args[0] klass.__table__ = table = Table( klass.__tablename__, cls.metadata, autoload=True, autoload_with=engine, *klass.extra_cols ) klass.__mapper__ = mapper(klass, table, **kw) class Bar(Base): __tablename__ = 'bar' extra_cols = [ Column('foo_id', Integer, ForeignKey('foo.id')), ] -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] declarative and late reflection?
On Dec 22, 2011, at 7:28 PM, Michael Bayer wrote: this could work really nicely with extend_existing, which has been enhanced in 0.7.4, but there seem to be some glitches preventing it from being super nice, so I can't get that to work right now. Just send in those columns via your own means: Those glitches have a pending patch in ticket 2356: http://www.sqlalchemy.org/trac/ticket/2356 See the modified example case there. In 0.7.5 your use case will work exactly as you intend, where you'll be able to add Column objects to your declared class that will take precedence over what's autoloaded. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.