[sqlalchemy] is it reasonable to have a __getattr__ method in class inheriting from declarative_base ?
Hello, I discovered SQLAlchemy yesterday, and have been trying to figure out if its worth using as I rewrite my clunky code to maintain sql records of parameter values of expensive data structures (large numpy arrays) stored in Python shelves. It looks fantastic, but not sure yet that it is the right tool. Because I use extensive use of __getattr__ in my classes as currently written, I added the __getattr__ method in this sample code from this tutorial (http://www.sqlalchemy.org/docs/05/ormtutorial.html). from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) name = Column(String) fullname = Column(String) password = Column(String) def __init__(self, name, fullname, password): self.name = name self.fullname = fullname self.password = password def __getattr__(self,attr): print called __getattr__ %s % attr def __repr__(self): return User('%s','%s', '%s') % (self.name, self.fullname, self.password) # I got this error: u = User('jon','jon jon','jj') called __getattr__ _sa_instance_state session.add(u) called __getattr__ _sa_instance_state Traceback (most recent call last): File stdin, line 1, in module File .../SQLAlchemy-0.5.0-py2.5.egg/sqlalchemy/orm/session.py, line 1096, in add self._save_or_update_state(state) File .../SQLAlchemy-0.5.0-py2.5.egg/sqlalchemy/orm/session.py, line 1105, in _save_or_update_state self._save_or_update_impl(state) File .../SQLAlchemy-0.5.0-py2.5.egg/sqlalchemy/orm/session.py, line 1264, in _save_or_update_impl if state.key is None: AttributeError: 'NoneType' object has no attribute 'key' then I changed to: def __getattr__(self,attr): print called __getattr__ %s % attr Base.__getattr__(attr) and session.add(u) runs without error. I'm definitely in over my head at this point, but I'd like to know if this is a reasonable solution or if I'm going to run problems with SQLAlchemy regarding my reliance on __getattr__ down the road. Any pointers would be much appreciated. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: mysql udfs
try func.yourfuncname(..) ? On Thursday 15 January 2009 03:18:51 clb wrote: Hi, I have been scouring the sqlalchemy 0.5 documentation and can't seem to find information on how to execute UDFs. I am dealing with the following scenario: 1. I create a UDF (for example, get_fav_id, which requires a varchar input). I create it directly in the database with mysql's CREATE FUNCTION. 2. I wish to execute it via sqlalchemy. I can execute statements such as: myengine.execute(select * from tablename;) Yet when I try to use the UDF (which is valid sql as far as mysql is concerned), nothing is returned: myengine.execute(select get_fav_id('input');) Any help would be appreciated. Thanks. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: Column('last_updated', onupdate=func.current_timestamp())
its not working. make sure you do in fact have the onupdate configured on the correct Column/Table, etc. On Jan 14, 2009, at 10:50 PM, atomburner wrote: Thank you for such a prompt response. Per the excellent documentation I added the following four lines to my .py script: import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) logging.getLogger('sqlalchemy.orm.unitofwork').setLevel (logging.DEBUG) And received the following log output: INFO:sqlalchemy.engine.base.Engine.0x...0350: SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), (SELECT substring(d.adsrc for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS DEFAULT, a.attnotnull, a.attnum, a.attrelid as table_oid FROM pg_catalog.pg_attribute a WHERE a.attrelid = ( SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE (pg_catalog.pg_table_is_visible(c.oid)) AND c.relname = %(table_name)s AND c.relkind in ('r','v') ) AND a.attnum 0 AND NOT a.attisdropped ORDER BY a.attnum INFO:sqlalchemy.engine.base.Engine.0x...0350:{'table_name': 'products'} INFO:sqlalchemy.engine.base.Engine.0x...0350: SELECT t.typname as name, pg_catalog.format_type(t.typbasetype, t.typtypmod) as attype, not t.typnotnull as nullable, t.typdefault as default, pg_catalog.pg_type_is_visible(t.oid) as visible, n.nspname as schema FROM pg_catalog.pg_type t LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace LEFT JOIN pg_catalog.pg_constraint r ON t.oid = r.contypid WHERE t.typtype = 'd' INFO:sqlalchemy.engine.base.Engine.0x...0350:{} INFO:sqlalchemy.engine.base.Engine.0x...0350: SELECT attname FROM pg_attribute WHERE attrelid = ( SELECT indexrelid FROM pg_index i WHERE i.indrelid = %(table)s AND i.indisprimary = 't') ORDER BY attnum INFO:sqlalchemy.engine.base.Engine.0x...0350:{'table': 16726} INFO:sqlalchemy.engine.base.Engine.0x...0350: SELECT conname, pg_catalog.pg_get_constraintdef(oid, true) as condef FROM pg_catalog.pg_constraint r WHERE r.conrelid = %(table)s AND r.contype = 'f' ORDER BY 1 INFO:sqlalchemy.engine.base.Engine.0x...0350:{'table': 16726} INFO:sqlalchemy.engine.base.Engine.0x...0350:UPDATE products SET title= %(title)s WHERE products.name = %(name_1)s INFO:sqlalchemy.engine.base.Engine.0x...0350:{'title': 'updated_title', 'name_1': 'test'} INFO:sqlalchemy.engine.base.Engine.0x...0350:COMMIT On Jan 14, 7:36 am, Michael Bayer mike...@zzzcomputing.com wrote: that should be fine, what does your log output say ? On Jan 14, 2009, at 12:38 AM, atomburner wrote: Mr. Bayer, you know entirely too much about all kinds of databases. A follow-up question to your erudite comment (and let me first say that I love sqlalchemy and have spent a good amount of time immersed in its documentation before posting): I tried running an update query of the form conn.execute(tbl.update().where(tbl.c.name=='test').values (title='updated_title')) FWIW the column was setup as follows (in PostgreSQL 8.2): Column('updated', DateTime,onupdate=func.now()) After running above code and requerying the db, the 'updated' column is still null. I also tried using func.current_timestamp() with no luck. Any tips you are able to provide are greatly appreciated. Best regards, Atom On Dec 6 2008, 6:48 am, Michael Bayer mike...@zzzcomputing.com wrote: that is the correct syntax. It will take effect any time an update() construct is used or when the ORM updates a row. Becauseonupdateis not aDDL-side construct, it will not take effect if you use a plain text UPDATE statement or if the update is otherwise not emitted by the SQLAlchemy application. The PassiveDefault, since it does represent DDL, will work in this manner, but only if the table was created using this table construct. On Dec 6, 2008, at 4:50 AM, jo wrote: Hi all, I created a table with the following column: Column('last_updated', DateTime, PassiveDefault(func.current_timestamp ()),onupdate=func.current_timestamp()) Maybe I don't understand howonupdateworks. I would like to have this column to be changed every time the row is updated, but it doesn't work. thank you for any help j --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group,
[sqlalchemy] Re: Error: attribute refresh operation cannot proceed
On Jan 15, 2:37 am, Michael Bayer mike...@zzzcomputing.com wrote: On Jan 14, 2009, at 3:01 PM, koranthala wrote: I understood your point and tried updating my code as mentioned. My code is a medium complex one - ~2K lines A problem that I see is that since I take the object and use it at many (~20) places, every single place I have to have a code like - m = session.merge(o) self.data[i] = m which is added. This seems very broilerplate - and there is no easy way to avoid it - without writing a get_session myself - or having a separate object for it. How is it handled in other projects? I would be very thankful if someone can help me out here. it seems more like the general approach of your application might need adjustment. I dont know what it means in your app to move an object to another thread. If there is just a general pool of objects that are used by any particular thread, each thread should acquire an object from the pool using a single method which takes a thread-bound Session, merges the requested object into the session, and returns it the merged copy. There should be no boilerplate and instead should be appropriate abstractions in place such that non-threadsafe data structures are not shared between threads concurrently I understand and accept. I modified the code appropriately and now I might have even solved a nasty bug too because of it:-) From reading the sqlalchemy doc, I was under the impression that session should be considered as a local variable. i.e. I was actually doing session = Session() in *every method* and then closing (session.close) it. Moreover, in places where I dont need to modify the variable, I was just calling the objects straightaway - and since the originating session is closed, it was actually creating a new session everytime. The bug I talked about--- I was finding that the memory was increasing quite a bit - My process is a long running one and actually within 1 hour, I used to use thrice the memory I started with. Most probably because a new session is created - and then not closed . Also, I used to find that after 1-3 hours the interpreter itself crashes. I ran the call after making the changes and till now (2 hrs and counting) the memory and interpreter is fine. Thank you very much Micheal. Regards --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] returning values as a list of values rather than as list of tuples
Hi, The following code returns a list of tuples to python from the db, corresponding to the values of the 'snpval_id' column in the table 'cell'. I was wondering if there was an easy way to have it return a list of values (in this case, integers) instead. * db = create_engine(postgres://btsnp:pqxxro...@localhost:5432/btsnp_full_genome) conn = db.connect() result = conn.execute(select snpval_id from cell where patient_chipid IN ('Duke1_plateC_F11.CEL')).fetchall() * value of result is [(2,), (1,), (-1,), (1,), (1,), (-1,)...] Please CC me on any reply. Thanks. Regards, Faheem Mitha. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: returning values as a list of values rather than as list of tuples
Hi Faheem, On Thu, Jan 15, 2009 at 11:05 AM, Faheem Mitha fah...@email.unc.edu wrote: Hi, The following code returns a list of tuples to python from the db, corresponding to the values of the 'snpval_id' column in the table 'cell'. I was wondering if there was an easy way to have it return a list of values (in this case, integers) instead. result = conn.execute(select snpval_id from cell where patient_chipid IN ('Duke1_plateC_F11.CEL')).fetchall() * Easiest thing is probably just to use a list comprehension: result_ints = [row[0] for row in result] Cheers, MZ --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: returning values as a list of values rather than as list of tuples
On Thu, 15 Jan 2009, Matthew Zwier wrote: Hi Faheem, On Thu, Jan 15, 2009 at 11:05 AM, Faheem Mitha fah...@email.unc.edu wrote: Hi, The following code returns a list of tuples to python from the db, corresponding to the values of the 'snpval_id' column in the table 'cell'. I was wondering if there was an easy way to have it return a list of values (in this case, integers) instead. result = conn.execute(select snpval_id from cell where patient_chipid IN ('Duke1_plateC_F11.CEL')).fetchall() * Easiest thing is probably just to use a list comprehension: result_ints = [row[0] for row in result] Hi Matthew, Yes, I'm doing that already. Just wondered if there was a way to return it in the right form directly. Regards, Faheem. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] cascade delete-orphan
Hi all, I try to set up a many-to-many relation with an association object. But I want something not usual: I want the child object deleted when not owned by any parent anymore. This is for a messages/recipients relation: the message is useless when everybody removed it from its mailbox! I tried that, but it doesn't work: class Parent(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Child(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Assoc(meta.DeclarativeBase): p_id = Column(types.Integer, ForeignKey(Parent.id)) c_id = Column(types.Integer, ForeignKey(Parent.id)) parent = relation(Parent, backref=backref('children', cascade='all, delete-orphan')) child = relation(Child, backref='parents', cascade='delete-orphan') I expect child = relation(Child, backref='parents', cascade='delete- orphan') to forward deletes to child when it is an orphan. But it looks like it forward the delete even if it is not an orphan yet... It that configuration: p1 = Parent() p2 = Parent() c = Child() assoc1 = Assoc(parent=p1, child=c) assoc2 = Assoc(parent=p2, child=c) p1.children = [ ] will lead to: - delete assoc1 (ok) - delete c (not ok) - update assoc2.c_id = null (not ok) So why is it not really a delete-orphan? :) Thanks GustaV --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: cascade delete-orphan
i think i had similar configuration, and i did it like one link was using default cascading, the other was just 'all'. but i'm not sure if yours is same, and any way i got mine via trial and error. do play with the cascade options. also i'm not sure if u apply some cascade on a backref what happens to the forward side. On Thursday 15 January 2009 18:30:29 GustaV wrote: Hi all, I try to set up a many-to-many relation with an association object. But I want something not usual: I want the child object deleted when not owned by any parent anymore. This is for a messages/recipients relation: the message is useless when everybody removed it from its mailbox! I tried that, but it doesn't work: class Parent(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Child(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Assoc(meta.DeclarativeBase): p_id = Column(types.Integer, ForeignKey(Parent.id)) c_id = Column(types.Integer, ForeignKey(Parent.id)) parent = relation(Parent, backref=backref('children', cascade='all, delete-orphan')) child = relation(Child, backref='parents', cascade='delete-orphan') I expect child = relation(Child, backref='parents', cascade='delete- orphan') to forward deletes to child when it is an orphan. But it looks like it forward the delete even if it is not an orphan yet... It that configuration: p1 = Parent() p2 = Parent() c = Child() assoc1 = Assoc(parent=p1, child=c) assoc2 = Assoc(parent=p2, child=c) p1.children = [ ] will lead to: - delete assoc1 (ok) - delete c (not ok) - update assoc2.c_id = null (not ok) So why is it not really a delete-orphan? :) Thanks GustaV --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: cascade delete-orphan
Is it planned for 0.6? or earlier? Anyway thanks: I stop trying all the combinations right now! :) On 15 jan, 18:30, Michael Bayer mike...@zzzcomputing.com wrote: There's various error conditions we should have added in 0.5 but have not. We should look into raising warnings in 0.5 and raising errors in 0.6. The conditions are, using delete-orphan without delete, and using delete-orphan with secondary. Neither is supported at this time. On Jan 15, 2009, at 11:30 AM, GustaV wrote: Hi all, I try to set up a many-to-many relation with an association object. But I want something not usual: I want the child object deleted when not owned by any parent anymore. This is for a messages/recipients relation: the message is useless when everybody removed it from its mailbox! I tried that, but it doesn't work: class Parent(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Child(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Assoc(meta.DeclarativeBase): p_id = Column(types.Integer, ForeignKey(Parent.id)) c_id = Column(types.Integer, ForeignKey(Parent.id)) parent = relation(Parent, backref=backref('children', cascade='all, delete-orphan')) child = relation(Child, backref='parents', cascade='delete-orphan') I expect child = relation(Child, backref='parents', cascade='delete- orphan') to forward deletes to child when it is an orphan. But it looks like it forward the delete even if it is not an orphan yet... It that configuration: p1 = Parent() p2 = Parent() c = Child() assoc1 = Assoc(parent=p1, child=c) assoc2 = Assoc(parent=p2, child=c) p1.children = [ ] will lead to: - delete assoc1 (ok) - delete c (not ok) - update assoc2.c_id = null (not ok) So why is it not really a delete-orphan? :) Thanks GustaV --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: cascade delete-orphan
i just committed the two warnings just now. On Jan 15, 2009, at 12:49 PM, GustaV wrote: Is it planned for 0.6? or earlier? Anyway thanks: I stop trying all the combinations right now! :) On 15 jan, 18:30, Michael Bayer mike...@zzzcomputing.com wrote: There's various error conditions we should have added in 0.5 but have not. We should look into raising warnings in 0.5 and raising errors in 0.6. The conditions are, using delete-orphan without delete, and using delete-orphan with secondary. Neither is supported at this time. On Jan 15, 2009, at 11:30 AM, GustaV wrote: Hi all, I try to set up a many-to-many relation with an association object. But I want something not usual: I want the child object deleted when not owned by any parent anymore. This is for a messages/recipients relation: the message is useless when everybody removed it from its mailbox! I tried that, but it doesn't work: class Parent(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Child(meta.DeclarativeBase): id = Column(types.Integer, primary_key=True) class Assoc(meta.DeclarativeBase): p_id = Column(types.Integer, ForeignKey(Parent.id)) c_id = Column(types.Integer, ForeignKey(Parent.id)) parent = relation(Parent, backref=backref('children', cascade='all, delete-orphan')) child = relation(Child, backref='parents', cascade='delete-orphan') I expect child = relation(Child, backref='parents', cascade='delete- orphan') to forward deletes to child when it is an orphan. But it looks like it forward the delete even if it is not an orphan yet... It that configuration: p1 = Parent() p2 = Parent() c = Child() assoc1 = Assoc(parent=p1, child=c) assoc2 = Assoc(parent=p2, child=c) p1.children = [ ] will lead to: - delete assoc1 (ok) - delete c (not ok) - update assoc2.c_id = null (not ok) So why is it not really a delete-orphan? :) Thanks GustaV --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] pyprocessing erros with SA
Hi, I am using SA inside of a couple of scripts that use pyprocessing to spawn off children. It looks like it's working, but occasionally I get some really odd errors. Here are the basics of the app: We are using the declarative layer, SA5, etc. At runtime, a parameter is passed in that gives the db to connect to. Pseudocode: Session = scoped_session(sessionmaker(autocommit=True, autoflush=True)) ... main.py Session.configure(bind=engine) manager = processing.Process(target=manager, name='manager', args=(mailing_id)) manager.start() manager.py from main import Session session = Session() worker = processing.Process(target=worker) worker.start() worker.py from main import Session session = Session() If I call Session.bind.dispose() at any point, any queries in the forked processes seem to get the following errors: ProgrammingError: (ProgrammingError) server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. Any ideas would be very helpful --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] creating indexes in MySQL
I have a quick question. I am trying to make an index for my table in MySQL. I have the line below: Index('index_table_on_field', table.c.field) I want to specify a length as in the SQL: KEY `index_table_on_field` (`field`(255)) Where do I enter the value 255 in the SQLAlchemy syntax? Thanks and I hope my question makes sense. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] utf8 encoding issue in MySQL
I have the table definition below: urls = Table('url', meta, Column('id', Integer(11), primary_key=True), Column('address', Unicode(1024)), Column('content', Unicode(255)), mysql_engine='InnoDB' ) This will make the SQL below: CREATE TABLE `url` ( `id` int(11) NOT NULL AUTO_INCREMENT, `address` varchar(1024) DEFAULT NULL, `content` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), ) ENGINE=InnoDB DEFAULT CHARSET=latin1 I want to know how I can specify the encoding for the table so that it generates the following: DEFAULT CHARSET=utf8 instead of DEFAULT CHARSET=latin1 as it is now. As it is now, SQLAlchemy stores the table using the latin1 encoding and converts the data back into Python unicode objects. This is fine as long as I use only SQLAlchemy. The thing is that my code and the table need to interact with other applications written by my colleague and thus the tables must be encoded with utf8. I cannot find the syntax to specify the encoding of the table. Thanks and I hope that my question makes sense. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: pyprocessing erros with SA
you should create a new engine per forked process, within the forked process, and bind it to the session.Using the connection pool from the parent process in a child fork does not produce behavior that's defined in any way I'm aware of (apparently the connections travel over for this particular DBAPI). Alternatively, you can try using the NullPool pool implementation which does not pool any connections. It will open and close connections for real without storing them in any kind of queue. On Jan 15, 2009, at 1:53 PM, mg wrote: Hi, I am using SA inside of a couple of scripts that use pyprocessing to spawn off children. It looks like it's working, but occasionally I get some really odd errors. Here are the basics of the app: We are using the declarative layer, SA5, etc. At runtime, a parameter is passed in that gives the db to connect to. Pseudocode: Session = scoped_session(sessionmaker(autocommit=True, autoflush=True)) ... main.py Session.configure(bind=engine) manager = processing.Process(target=manager, name='manager', args=(mailing_id)) manager.start() manager.py from main import Session session = Session() worker = processing.Process(target=worker) worker.start() worker.py from main import Session session = Session() If I call Session.bind.dispose() at any point, any queries in the forked processes seem to get the following errors: ProgrammingError: (ProgrammingError) server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. Any ideas would be very helpful --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: ORM base class for 0.5?
The declarative extension (sqlalchemy.ext.declarative) provides a __init__ that takes keyword args for attributes (at least it does in 0.4). On Tue, Jan 13, 2009 at 4:02 AM, Christoph Haas em...@christoph-haas.de wrote: Thanks for the code. For those who might also be interested in an ORM base class providing __init__, update and __repr__ - this is what I use now with 0.5 (comments welcome): = import sqlalchemy as sql from sqlalchemy import orm class MyOrm(object): def __init__(self, **kw): Create a mapped object with preset attributes for key, value in kw.iteritems(): if hasattr(self, key): setattr(self, key, value) elif not ignore_missing_columns: raise AttributeError('Cannot set attribute which is not column in mapped table: %s' % (key,)) def update(self, update_dict, ignore_missing_columns=True): Update an object's attributes from a dictionary for key, value in update_dict.iteritems(): if hasattr(self, key): setattr(self, key, value) elif not ignore_missing_columns: raise AttributeError('Cannot set attribute which is not column in mapped table: %s' % (key,)) def __repr__(self): Return a decent printable representation of a mapped object and its attributes. atts = [] columns = orm.object_mapper(self).mapped_table.c for column in columns: key = column.key if hasattr(self, key): col = columns.get(key) if not (getattr(col, 'server_default', None) is not None or isinstance(getattr(col, 'default', None), sql.PassiveDefault) or getattr(self, key) is None): atts.append( (key, getattr(self, key)) ) return self.__class__.__name__ + '(' + ', '.join(x[0] + '=' + repr(x[1]) for x in atts) + ')' = Would be nice if mapped objects could automatically get such methods assigned. Not sure if SQLAlchemy can or should provide that or if it broke other functionality. Cheers Christoph --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: utf8 encoding issue in MySQL
I think this is configurable on the MySQL server directly, i.e. default charset, otherwise you can set it via **{'mysql_DEFAULT CHARSET':'utf8'} in your Table def. On Jan 15, 2009, at 3:17 PM, KevinTran wrote: I have the table definition below: urls = Table('url', meta, Column('id', Integer(11), primary_key=True), Column('address', Unicode(1024)), Column('content', Unicode(255)), mysql_engine='InnoDB' ) This will make the SQL below: CREATE TABLE `url` ( `id` int(11) NOT NULL AUTO_INCREMENT, `address` varchar(1024) DEFAULT NULL, `content` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`), ) ENGINE=InnoDB DEFAULT CHARSET=latin1 I want to know how I can specify the encoding for the table so that it generates the following: DEFAULT CHARSET=utf8 instead of DEFAULT CHARSET=latin1 as it is now. As it is now, SQLAlchemy stores the table using the latin1 encoding and converts the data back into Python unicode objects. This is fine as long as I use only SQLAlchemy. The thing is that my code and the table need to interact with other applications written by my colleague and thus the tables must be encoded with utf8. I cannot find the syntax to specify the encoding of the table. Thanks and I hope that my question makes sense. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: creating indexes in MySQL
try using the DDL() construct instead. I dont think Index() supports that syntax. On Jan 15, 2009, at 3:10 PM, KevinTran wrote: I have a quick question. I am trying to make an index for my table in MySQL. I have the line below: Index('index_table_on_field', table.c.field) I want to specify a length as in the SQL: KEY `index_table_on_field` (`field`(255)) Where do I enter the value 255 in the SQLAlchemy syntax? Thanks and I hope my question makes sense. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: returning values as a list of values rather than as list of tuples
I've always thought this format for the list comprehension was particularly clean: result = [x for (x, ) in conn.execute(.).fetchall()] On Jan 15, 8:27 am, Faheem Mitha fah...@email.unc.edu wrote: On Thu, 15 Jan 2009, Matthew Zwier wrote: Hi Faheem, On Thu, Jan 15, 2009 at 11:05 AM, Faheem Mitha fah...@email.unc.edu wrote: Hi, The following code returns a list of tuples to python from the db, corresponding to the values of the 'snpval_id' column in the table 'cell'. I was wondering if there was an easy way to have it return a list of values (in this case, integers) instead. result = conn.execute(select snpval_id from cell where patient_chipid IN ('Duke1_plateC_F11.CEL')).fetchall() * Easiest thing is probably just to use a list comprehension: result_ints = [row[0] for row in result] Hi Matthew, Yes, I'm doing that already. Just wondered if there was a way to return it in the right form directly. Regards, Faheem. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: cascade delete-orphan
What is the best way to achieve the auto delete of the child then (refering to the example earlier)? I think about using an attribute extension... --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---