[sqlalchemy] Re: threadlocal sessions and transactions
On Jul 19, 2008, at 10:32 AM, huy do wrote: > >> If >> you continue to use the threadlocal engine strategy, then implicit >> executions (i.e. somestatement.execute()) should participate in the >> same transaction as the Session. > > Is there a warning in that "If" ? Is the threadlocal strategy not the > most popular for web programs ? > Theres nothing wrong with using the threadlocal strategy, but I think scoped_session() is more widely used just because its a little more transparent in its usage. --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] integration of SQLAlchemy in a WSI environ
I have completed my idea of integration of SQLALchemy in a WSGI environ, in my wsgix framework. wsgix (http://hg.mperillo.ath.cx/wsgix) is a WSGI framework whose main design goal it to provide a low level but flexible layer over WSGI. Since WSGI is mainly a functional API, wsgix is the same: a collection of functions that explicitly use the WSGI environ to keep state (and also configuration options). The integration with SQLALchemy is in wsgix.dbapi (http://hg.mperillo.ath.cx/wsgix/file/tip/wsgix/dbapi/__init__.py). The integration consist only of two pair of functions: contextual_connect(environ, engine) close_contextual_connection(environ, engine) and scoped_session(environ, session_factory) close_scoped_session(environ) These functions use the WSGI environ dictionary to keep the state, and a reference count is used to make sure resources are closed when no more needed. There are also some high level wrappers around contextual_connect. An example of high level wrapper around scoped_session can be found instead in the test suite (since I'm not sure about the best patterns for advanced session usage): http://hg.mperillo.ath.cx/wsgix/file/tip/wsgix/test/dbapi/test_dbapi.py I would really appreciate comments about the code. Thanks Manlio Perillo --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Boolean, Declerative, MySQL 5.2
> I am using Column(Boolean) with declarative and MySQL and it is > working fine. In MySQL itself the type is 'tinyint(1)' but they > provide 'bool' and 'boolean' as synonyms if you prefer. Bobby, Thank you for this, I went with the tinyint(1) and it seems to be working great! Thanks, Heston --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: threadlocal sessions and transactions
On Jul 20, 12:13 am, Michael Bayer <[EMAIL PROTECTED]> wrote: > On Jul 19, 2008, at 9:10 AM, Contact 42 wrote: > > > > > > > Hi, > > > I am using sa 0.5b2 under python 2.5. > > > Should the following code commit myobj. > > > engine = create_engine(appconfig.dburi, strategy='threadlocal') > > Session = scoped_session(sessionmaker(bind=engine, autoflush=True, > > autocommit=True)) > > > sess = Session() > > engine.begin() > > sess.add(myobj) > > engine.commit() > > > I am noticing that it does not. However, if I do > > > sess = Session() > > sess.begin() > > sess.add(myobj) > > sess.commit() > > > it works. > > > Am I missing something ? > > The "commit()" method on Session has the additional functionality of > flushing itself when called. If you want to use Session within a > threadlocal transaction at the engine level, you'd have to ensure that > you flush() the session manually before commit. I thought autoflush=True would do that for me. This is not a problem, I'll just use the session transaction interface. > > These days the Session has become more prominent as the center of > transactional control, however, so you might find it easier to just go > through scoped_session() for transaction control entirely. Session > has an execute() and connection() method for issuing raw SQL too. Yeah this is great. I will move all my engine stuff to use the session directly. > If > you continue to use the threadlocal engine strategy, then implicit > executions (i.e. somestatement.execute()) should participate in the > same transaction as the Session. Is there a warning in that "If" ? Is the threadlocal strategy not the most popular for web programs ? Thanks, Huy --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Custom Table
On Jul 19, 2008, at 5:54 AM, HellRaiser wrote: > > Hello > > I like to implement a custom Table object, which automatically add > some custome rows to the Table the User create. > > #!/usr/bin/env python > #-*- coding: utf-8 -*- > > from addressmanager.model import DBSession > from sqlalchemy import Table, Column, Integer, ForeignKey, and_ > > > class MandateTable(Table): >def __init__(self, name, metadata, *args, **kwargs): >Table.__init__(self, name, metadata, *args, > **kwargs) >self.append_column(Column("mandate_id", Integer, > ForeignKey("mandates.mandate_id"), nullable=False)) > >def query(self, query, mandate): >return DBSession.query(self).filter(and_(query, "idmand=%i" % > mandate.mandate_id)) > > Something llike that. > But now, when i start the application I got an error, that there is no > Table object (ähh? but it inherit from Table). > > Is there a best practise to extend the table object? or to > automatically add custom rows to a Table? Table is not easy to subclass due to the metaclass which controls its creational pattern. Since all you need here is a creational pattern, use a def instead: def MandateTable(*args, **kwargs): args = args + (Column("mandate_id", Integer, ...),) return Table(*args, **kwargs) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] threadlocal sessions and transactions
Hi, I am using sa 0.5b2 under python 2.5. Should the following code commit myobj. engine = create_engine(appconfig.dburi, strategy='threadlocal') Session = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=True)) sess = Session() engine.begin() sess.add(myobj) engine.commit() I am noticing that it does not. However, if I do sess = Session() sess.begin() sess.add(myobj) sess.commit() it works. Am I missing something ? Thanks, Huy --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Custom Table
Hello I like to implement a custom Table object, which automatically add some custome rows to the Table the User create. #!/usr/bin/env python #-*- coding: utf-8 -*- from addressmanager.model import DBSession from sqlalchemy import Table, Column, Integer, ForeignKey, and_ class MandateTable(Table): def __init__(self, name, metadata, *args, **kwargs): Table.__init__(self, name, metadata, *args, **kwargs) self.append_column(Column("mandate_id", Integer, ForeignKey("mandates.mandate_id"), nullable=False)) def query(self, query, mandate): return DBSession.query(self).filter(and_(query, "idmand=%i" % mandate.mandate_id)) Something llike that. But now, when i start the application I got an error, that there is no Table object (ähh? but it inherit from Table). Is there a best practise to extend the table object? or to automatically add custom rows to a Table? --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Boolean, Declerative, MySQL 5.2
Hello Guys, I'm looking to store a Boolean value in a MySQL 5.2 database. I'm then going to describe a class for the table using declarative and have a couple of questions on this: What Datatype should my table column be set to in MySQL? And likewise, when declaring the column using declarative, which data type should I use? Column(Boolean)? Cheers guys, Heston --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: Overriding table columns with Python-property
Michael Bayer wrote: > works for me: I tried adapting your example, which admittedly works :-), to a scenario that better resembles mine, but now the property is overriden simply, even when I use ``exclude_properties``. Note that the setup is overly complex, but this should be seen in the light of a larger setup (as you've previously guided me towards, incidentally). from sqlalchemy import * from sqlalchemy.orm import * e = create_engine('sqlite://') m = MetaData(e) t1= Table( 't1', m, Column('id', Integer, primary_key=True), Column('col', String(50)), ) t1.create() t2= Table( 't2', m, Column('id', Integer, ForeignKey("t1.id"), primary_key=True), Column('data', String(50)), ) t2.create() class T1(object): pass class T2(T1): @property def col(self): return u"Some read-only value." polymorphic = ( [T2], t1.join(t2)) mapper(T1, t1) mapper( T2, t2, exclude_properties=('col',), with_polymorphic=polymorphic, inherits=T1, inherit_condition=(t1.c.id==t2.c.id), ) sess = sessionmaker()() x = T2() assert type(T2.col) is property x.data = "some data" sess.save(x) sess.commit() sess.clear() assert sess.query(T2).one().data == "some data" assert sess.query(T2).one().col == u"Some read-only value." x = sess.query(T2).one() x.data = "some new data" sess.commit() assert sess.query(T2).one().data == "some new data" \malthe --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---