Re: [sqlalchemy] Filtering on Postgresql BigInteger array
You need to use the cast() function here to do an explicit cast on the SQL side. array([cast(node.id, BigInteger)]). The type_ argument in most other cases only describes the type on the Python side. Sent from my iPhone On Apr 16, 2013, at 12:42 PM, Alexander Bachmeier alexander.bachme...@gmail.com wrote: I'm trying to query an array of bigint[] using SQLAlchemy 0.8 on a Postgresql database. My query looks like this: ways_with_node = db.query(Ways).filter(Ways.nodes.contains(array([node1.id], type_=BigInteger))).all() and I'm getting the following exception: cursor.execute(statement, parameters) sqlalchemy.exc.ProgrammingError: (ProgrammingError) operator does not exist: bigint[] @ integer[] LINE 3: WHERE ways.nodes @ ARRAY[ -710] It looks like the array() function isn't changing the type to BigInteger. I've tried CHAR and the second argument remains an integer[] array. The query should end up looking something like this: select * from ways where nodes @ '{-710}'::bigint[] Which successfully executes as plain SQL. I've tried different variations of the query, including: db.query(Ways).filter(Ways.nodes.op('@')(array([node1.id], type_=BigInteger))).all() but the result is the same exception. Since I have run out of ideas, I was wondering if there is a bug in the array() function or if I'm doing something wrong here. -- 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?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 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Filtering on Postgresql BigInteger array
On Tuesday, April 16, 2013 6:58:15 PM UTC+2, Michael Bayer wrote: You need to use the cast() function here to do an explicit cast on the SQL side. array([cast(node.id, BigInteger)]). The type_ argument in most other cases only describes the type on the Python side. Thanks a lot, that fixed it for me. I was already working with the cast() before I found array and couldn't get it to work. I just never had the idea to combine those two. -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] Session.add doesn't work on Python 3.3.0?
I have a strange error using Python 3.3.0 and SQLAlchemy 0.8.0: from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKeyfrom sqlalchemy.orm import sessionmaker, relationship, backreffrom sqlalchemy.ext.declarative import declarative_base engine = create_engine(sqlite:///:memory:, echo=True)Session = sessionmaker(bind = engine)Base = declarative_base() class Product(Base): __tablename__ = products id = Column(Integer, primary_key=True) name = Column(String(200)) description = Column(Text) Base.metadata.create_all(engine) session = Session() product = Product() product.id = 1 product.name = Test product.description = Test session.add(product)print(product in session) # Expected true session.commit()print(session.query(Product).all()) # Expected the previous item As commented in code I expected the add method to add the product to the database using an INSERT. But it doesn't nor does it throw an exception. This is the log: 2013-04-16 18:03:14,368 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(products)2013-04-16 18:03:14,369 INFO sqlalchemy.engine.base.Engine ()2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine CREATE TABLE products ( id INTEGER NOT NULL, name VARCHAR(200), description TEXT, PRIMARY KEY (id)) 2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine ()2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine COMMITFalse2013-04-16 18:04:04,706 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)2013-04-16 18:04:04,708 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.description AS products_description FROM products2013-04-16 18:04:04,709 INFO sqlalchemy.engine.base.Engine ()[] I already tried several things: - Changing the engine doesn't work: I also tried postgresql. - I tried debugging. When I reach the body of the add method there is nothing to step into. Am I doing something wrong or is this a bug? (Also posted this question on Stack Overflow: http://stackoverflow.com/questions/16041868/sqlalchemy-session-add-doesnt-work ) -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Session.add doesn't work on Python 3.3.0?
I think you need to commit your session data. Em terça-feira, 16 de abril de 2013, Tim Cooijmans escreveu: I have a strange error using Python 3.3.0 and SQLAlchemy 0.8.0: from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKeyfrom sqlalchemy.orm import sessionmaker, relationship, backreffrom sqlalchemy.ext.declarative import declarative_base engine = create_engine(sqlite:///:memory:, echo=True)Session = sessionmaker(bind = engine)Base = declarative_base() class Product(Base): __tablename__ = products id = Column(Integer, primary_key=True) name = Column(String(200)) description = Column(Text) Base.metadata.create_all(engine) session = Session() product = Product() product.id = 1 product.name = Test product.description = Test session.add(product)print(product in session) # Expected true session.commit()print(session.query(Product).all()) # Expected the previous item As commented in code I expected the add method to add the product to the database using an INSERT. But it doesn't nor does it throw an exception. This is the log: 2013-04-16 18:03:14,368 INFO sqlalchemy.engine.base.Engine PRAGMA table_info(products)2013-04-16 18:03:14,369 INFO sqlalchemy.engine.base.Engine ()2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine CREATE TABLE products ( id INTEGER NOT NULL, name VARCHAR(200), description TEXT, PRIMARY KEY (id)) 2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine ()2013-04-16 18:03:14,371 INFO sqlalchemy.engine.base.Engine COMMITFalse2013-04-16 18:04 -- *Mauricio de Abreu Antunes* Mobile: (51)930-74-525 Skype: mauricio.abreua -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Session.add doesn't work on Python 3.3.0?
cant reproduce, running with Python 3.3.0 here against 0.8.0 I get the INSERT: PRAGMA table_info(products) 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine () 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine CREATE TABLE products ( id INTEGER NOT NULL, name VARCHAR(200), description TEXT, PRIMARY KEY (id) ) 2013-04-16 16:14:41,020 INFO sqlalchemy.engine.base.Engine () 2013-04-16 16:14:41,020 INFO sqlalchemy.engine.base.Engine COMMIT True 2013-04-16 16:14:41,021 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-04-16 16:14:41,021 INFO sqlalchemy.engine.base.Engine INSERT INTO products (id, name, description) VALUES (?, ?, ?) 2013-04-16 16:14:41,021 INFO sqlalchemy.engine.base.Engine (1, 'Test', 'Test') 2013-04-16 16:14:41,022 INFO sqlalchemy.engine.base.Engine COMMIT 2013-04-16 16:14:41,022 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-04-16 16:14:41,022 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.description AS products_description FROM products 2013-04-16 16:14:41,023 INFO sqlalchemy.engine.base.Engine () [__main__.Product object at 0x105c3dbd0] On Apr 16, 2013, at 2:42 PM, Tim Cooijmans timcooijm...@gmail.com wrote: from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKey from sqlalchemy.orm import sessionmaker, relationship, backref from sqlalchemy.ext.declarative import declarative_base engine = create_engine(sqlite:///:memory:, echo=True) Session = sessionmaker(bind = engine) Base = declarative_base() class Product(Base): __tablename__ = products id = Column(Integer, primary_key=True) name = Column(String(200)) description = Column(Text) Base.metadata.create_all(engine) session = Session() product = Product() product.id = 1 product.name = Test product.description = Test session.add(product) print(product in session) # Expected true session.commit() print(session.query(Product).all()) # Expected the previous item -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Session.add doesn't work on Python 3.3.0?
I tried your same code here I got True. Em terça-feira, 16 de abril de 2013, Michael Bayer escreveu: cant reproduce, running with Python 3.3.0 here against 0.8.0 I get the INSERT: PRAGMA table_info(products) 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine () 2013-04-16 16:14:41,019 INFO sqlalchemy.engine.base.Engine CREATE TABLE products ( id INTEGER NOT NULL, name VARCHAR(200), description TEXT, PRIMARY KEY (id) ) 2013-04-16 16:14:41,020 INFO sqlalchemy.engine.base.Engine () 2013-04-16 16:14:41,020 INFO sqlalchemy.engine.base.Engine COMMIT True 2013-04-16 16:14:41,021 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-04-16 16:14:41,021 INFO sqlalchemy.engine.base.Engine INSERT INTO products (id, name, description) VALUES (?, ?, ?) 2013-04-16 16:14:41,021 INFO sqlalchemy.engine.base.Engine (1, 'Test', 'Test') 2013-04-16 16:14:41,022 INFO sqlalchemy.engine.base.Engine COMMIT 2013-04-16 16:14:41,022 INFO sqlalchemy.engine.base.Engine BEGIN (implicit) 2013-04-16 16:14:41,022 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.description AS products_description FROM products 2013-04-16 16:14:41,023 INFO sqlalchemy.engine.base.Engine () [__main__.Product object at 0x105c3dbd0] On Apr 16, 2013, at 2:42 PM, Tim Cooijmans timcooijm...@gmail.com wrote: from sqlalchemy import create_engine, Integer, String, Text, Column, Sequence, DateTime, ForeignKeyfrom sqlalchemy.orm import sessionmaker, relationship, backreffrom sqlalchemy.ext.declarative import declarative_base engine = create_engine(sqlite:///:memory:, echo=True)Session = sessionmaker(bind = engine)Base = declarative_base() -- 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 javascript:_e({}, 'cvml', 'sqlalchemy%2bunsubscr...@googlegroups.com');. To post to this group, send email to sqlalchemy@googlegroups.comjavascript:_e({}, 'cvml', 'sqlalchemy@googlegroups.com'); . Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out. -- *Mauricio de Abreu Antunes* Mobile: (51)930-74-525 Skype: mauricio.abreua -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] Querying using objects
Hello all! Ok, maybe people asks this a lot, but I wonder if it's possible to perform a query using an object as a filter - and I searched for it, didn't found anything close to my idea. Simple dumb example code: *class User(Base):** ** ** user_id = Column(Integer, Sequence(...), primary_key=True)** ** username = Column(Unicode)** ** ** **class Subscription(Base):** ** **subscription_id = Column(Integer, Sequence(...), primary_key=True)** **name = Column(unicode)** **owner_id = Column(Integer, ForeignKey('user.user_id'), nullable=False)** ** **@hybrid_property** **def owner(self):** if not object_session(self): return None **return object_session(self).query(User).filter(self.owner_id == User.user_id)**.first() ** **@owner.setter** **def owner(self, owner):** **self.owner_id = owner.user_id if isinstance(owner, User) else owner** **if object_session(self):** **object_session(self).commit()** ** **# @owner.expression** # ???** ** ** **# ok, so far *almost* good** ** **new_user = User()** **new_user.username = u'the user'** ** **session.add(new_user)** **session.commit()** ** **subscription = Subscription()** **subscription.name = u'the subscription'** **subscription.owner_id = new_user.user_id** ** **session.add(subscription)** **session.commit()** ** **# then, it is normal to query for subscriptions owned by new_user like this** ** **print session.query(Subscription).filter(Subscription.owner_id == new_user.user_id).all()** ** **# but, i would like to do _this_ instead** ** **print session.query(Subscription).filter(Subscription.owner == new_user).all()* I've tried it in so many ways that I feel dizzy. The only way I think would be using @owner.expression to return User, but that didn't the trick, it only appends WHERE false to the query, hehehe. Any light on my way? :) 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 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] SQLAlchemy introspection of relationships
This has also been posted here: http://stackoverflow.com/questions/15950375/sqlalchemy-introspection-of-relationships, shoulda asked here first. I have a use case for modelling data in a SQL schema rather than through defining SQLAlchemy objects. However, i require a way to construct the corresponding SQLAlchemy objects automatically from the SQL schema. Currently i have: from sqlalchemy import MetaData from sqlalchemy.ext.declarative import declarative_base meta = MetaData(bind=your_engine) meta.reflect() # get the table which we would like to represent as a Python object table = meta.tables['your_table_name'] # define the object which we will use as the base class base = declarative_base() # create db object MyObject = type(str(table.fullname), (base,), {'__table__': table}) however, the relationships between objects are not created. Any ideas how to go about doing this? 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. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] SQLAlchemy introspection of relationships
same answer: sqlalchemy has no built in facility for generating relationship() structures automatically. there's a library called sqlautocode that does this but I don't know its status. On Apr 12, 2013, at 11:03 AM, ken cmor...@reddeer.gb.com wrote: This has also been posted here: http://stackoverflow.com/questions/15950375/sqlalchemy-introspection-of-relationships, shoulda asked here first. I have a use case for modelling data in a SQL schema rather than through defining SQLAlchemy objects. However, i require a way to construct the corresponding SQLAlchemy objects automatically from the SQL schema. Currently i have: from sqlalchemy import MetaData from sqlalchemy.ext.declarative import declarative_base meta = MetaData(bind=your_engine) meta.reflect() # get the table which we would like to represent as a Python object table = meta.tables['your_table_name'] # define the object which we will use as the base class base = declarative_base() # create db object MyObject = type(str(table.fullname), (base,), {'__table__': table}) however, the relationships between objects are not created. Any ideas how to go about doing this? 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. To post to this group, send email to sqlalchemy@googlegroups.com. 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 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Querying using objects
On Apr 16, 2013, at 9:30 AM, Richard Gerd Kuesters rich...@humantech.com.br wrote: Hello all! Ok, maybe people asks this a lot, but I wonder if it's possible to perform a query using an object as a filter - and I searched for it, didn't found anything close to my idea. Simple dumb example code: class User(Base): user_id = Column(Integer, Sequence(...), primary_key=True) username = Column(Unicode) class Subscription(Base): subscription_id = Column(Integer, Sequence(...), primary_key=True) name = Column(unicode) owner_id = Column(Integer, ForeignKey('user.user_id'), nullable=False) @hybrid_property def owner(self): if not object_session(self): return None return object_session(self).query(User).filter(self.owner_id == User.user_id).first() @owner.setter def owner(self, owner): self.owner_id = owner.user_id if isinstance(owner, User) else owner if object_session(self): object_session(self).commit() # @owner.expression # ??? # ok, so far *almost* good OK, all of that complexity with hybrid_property is not needed at all here. Just say, owner = relationship(User), and you're done. SQLAlchemy manages one-to-many, many-to-one, and many-to-many automatically with relationship(). # but, i would like to do _this_ instead print session.query(Subscription).filter(Subscription.owner == new_user).all() I've tried it in so many ways that I feel dizzy. The only way I think would be using @owner.expression to return User, but that didn't the trick, it only appends WHERE false to the query, hehehe. yeah just use relationship(), and you'd be using the first operator as described right here in the ORM tutorial (which is a must-read): http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html#common-relationship-operators -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
[sqlalchemy] How to map a oracle table with long table name
Hello all, I want to map a table whose name is BAND_ORDER_OF_LOCAL_TESTING. class BandOrderOfLocalTesting(Base): __TABLENAME__ = 'BAND_ORDER_OF_LOCAL_TESTING' order_id = Column(order_id, Number(18), primary_key=True) ... Each time I got the following message: 013-04-17 09:26:55,857 INFO sqlalchemy.engine.base.Engine {'ROWNUM_1': 1} Traceback (most recent call last): File tables.py, line 108, in module instance = session.query(BandOrderOfMonth).first() File /usr/lib/python2.7/site-packages/SQLAlchemy-0.8.1dev-py2.7.egg/sqlalchemy/orm/query.py, line 2181, in first ret = list(self[0:1]) File /usr/lib/python2.7/site-packages/SQLAlchemy-0.8.1dev-py2.7.egg/sqlalchemy/orm/query.py, line 2048, in __getitem__ return list(res) File /usr/lib/python2.7/site-packages/SQLAlchemy-0.8.1dev-py2.7.egg/sqlalchemy/orm/loading.py, line 72, in instances rows = [process[0](row, None) for row in fetch] File /usr/lib/python2.7/site-packages/SQLAlchemy-0.8.1dev-py2.7.egg/sqlalchemy/orm/loading.py, line 356, in _instance tuple([row[column] for column in pk_cols]) File /usr/lib/python2.7/site-packages/SQLAlchemy-0.8.1dev-py2.7.egg/sqlalchemy/engine/result.py, line 71, in __getitem__ processor, obj, index = self._parent._key_fallback(key) File /usr/lib/python2.7/site-packages/SQLAlchemy-0.8.1dev-py2.7.egg/sqlalchemy/engine/result.py, line 314, in _key_fallback expression._string_or_unprintable(key)) sqlalchemy.exc.NoSuchColumnError: Could not locate column in row for column 'BAND_ORDER_OF_LOCAL_TESTING.order_id' Is there a limit of table name in sqlalchemy? How to solve this problem? Best regards, Evan -- 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?hl=en. For more options, visit https://groups.google.com/groups/opt_out.