[sqlalchemy] Re: no error when using wrong 'attribute' name on an object
Michael Bayer a écrit : well, theres nothing that says the attribute youre sending in is wrong. classes in python dont have any notion of predeclared attribute names. a mapped class can have any number of other attributes which dont correspond to database-mapped attributes. Yes, of course. I was focused on the database... in your case, you would like to constrain the attributes on your class to the set of those which have been explicitly set up in a mapping relationship. youd implement your own constructor like this: class MyBaseClass(ActiveMapper): def __init__(self, **kwargs): mapper = class_mapper(self.__class__) for key, value in kwargs.items(): if key not in mapper.props: raise AttributeError(non mapped attribute: '%s' % key) setattr(self, key, value) class Color(MyBaseClass): # etc I'll try this approach. Thanks. while im not a big ActiveMapper user, id leave it up to the ActiveMapper developers if they think this behavior should be built in to ActiveMapper itself (i kind of dont think it should be). --~--~-~--~~~---~--~~ 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] DynamicMetaData + create/drop table = confusion
hello. I'm trying to use DynamicMetaData so that two separate tests can each connect to a unique db, create some tables, insert some data, delete that data, then drop the tables. This seems to yield ... sqlalchemy.exceptions.ConcurrentModificationError: Updated rowcount 0 does not match number of objects updated 1 ... the second time around, when trying to delete the data. I checked the list and FAQ and note that I am *not* trying to modify a primary key. This seems related to the use of DynamicMetaData and the fact that I am creating the table then dropping it. I was able to reproduce this scenario in a single test (below and attached) so maybe you can see something simple I'm doing wrong? Experimenting, I noticed that if I use two separate BoundMetaData instances *or* switch to checkfirst=True and not drop the table then the test works fine. However, I don't see why this shouldn't be possible with DynamicMetaData. Thanks in advance. PS. this was in sqlalchemy trunk r 2183 _ import sqlalchemy from sqlalchemy import * from sqlalchemy.ext.assignmapper import assign_mapper from sqlalchemy.ext.sessioncontext import SessionContext def eq_(a,b): assert a==b, %s != %s % (a,b) meta = DynamicMetaData() offers = Table(offers, meta, Column(id, INT, primary_key=True), Column(name, String ), ) class Offer(object): pass def db_roundtrip(dsn): conn = meta.connect(dsn) meta.engine.echo = 1 context = SessionContext( lambda: sqlalchemy.create_session(bind_to=meta.engine)) assign_mapper(context, Offer, offers) session = context.current meta.create_all() session.flush() offer = Offer() offer.name = 'foobar' session.save(offer) session.flush() rows = Offer.select() eq_(len(rows), 1) eq_(rows[0].id, 1) eq_(rows[0].name, 'foobar') session.delete(offer) session.flush() rows = Offer.select() eq_(len(rows), 0) meta.drop_all() session.flush() sqlalchemy.orm.clear_mappers() if __name__ == '__main__': db_roundtrip('sqlite:///:memory:') # pretend this is another connection : db_roundtrip('sqlite:///:memory:') print 'OK' --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~--- test_sa_concurrent.py Description: Binary data
[sqlalchemy] Re: DynamicMetaData + create/drop table = confusion
when i run this program, I get no errors. a few things to note: it makes no sense to do a session.flush() after youve dropped the table (similarly, it makes no sense to flush() before youve done anything but thats less of an issue). if there were any modified state within your session to actually be flushed, the operation would fail since the table is dropped (but in this test, theres no changes to flush at that point, so no error occurs). running the same test twice with two separate instances of sqlite:// memory will produce no continuity between the two databases, since sqlite://memory databases are completely unique per connection. but again, in this test youre doing a completely new and unrelated set of operations on each connection, so no error occurs. wild guess, that the test you have which fails is switching between sqlite://memory databases within a single ORM session, so the update of a row saved within the first sqlite://memory database produces no changes within the second sqlite://memory database since its a completely new database (but then, youd also have to get the table creates in there too, so still not exactly sure what the conditions are on your end). in any case, viewing SQL output should reveal exactly whats going on. On Jan 6, 2007, at 6:54 PM, Kumar McMillan wrote: hello. I'm trying to use DynamicMetaData so that two separate tests can each connect to a unique db, create some tables, insert some data, delete that data, then drop the tables. This seems to yield ... sqlalchemy.exceptions.ConcurrentModificationError: Updated rowcount 0 does not match number of objects updated 1 ... the second time around, when trying to delete the data. I checked the list and FAQ and note that I am *not* trying to modify a primary key. This seems related to the use of DynamicMetaData and the fact that I am creating the table then dropping it. I was able to reproduce this scenario in a single test (below and attached) so maybe you can see something simple I'm doing wrong? Experimenting, I noticed that if I use two separate BoundMetaData instances *or* switch to checkfirst=True and not drop the table then the test works fine. However, I don't see why this shouldn't be possible with DynamicMetaData. Thanks in advance. PS. this was in sqlalchemy trunk r 2183 _ import sqlalchemy from sqlalchemy import * from sqlalchemy.ext.assignmapper import assign_mapper from sqlalchemy.ext.sessioncontext import SessionContext def eq_(a,b): assert a==b, %s != %s % (a,b) meta = DynamicMetaData() offers = Table(offers, meta, Column(id, INT, primary_key=True), Column(name, String ), ) class Offer(object): pass def db_roundtrip(dsn): conn = meta.connect(dsn) meta.engine.echo = 1 context = SessionContext( lambda: sqlalchemy.create_session(bind_to=meta.engine)) assign_mapper(context, Offer, offers) session = context.current meta.create_all() session.flush() offer = Offer() offer.name = 'foobar' session.save(offer) session.flush() rows = Offer.select() eq_(len(rows), 1) eq_(rows[0].id, 1) eq_(rows[0].name, 'foobar') session.delete(offer) session.flush() rows = Offer.select() eq_(len(rows), 0) meta.drop_all() session.flush() sqlalchemy.orm.clear_mappers() if __name__ == '__main__': db_roundtrip('sqlite:///:memory:') # pretend this is another connection : db_roundtrip('sqlite:///:memory:') print 'OK' test_sa_concurrent.py --~--~-~--~~~---~--~~ 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: How to order by a field in a different table?
A query that doesn't rely on broken GROUP BY might look like select([forum_topics, select([func.max(forum_posts.c.created)], forum_posts.c.topic_id==forum_topics.c.id, scalar=True).label('last_post')], order_by=[desc('last_post')]) On 12/28/06, Mart [EMAIL PROTECTED] wrote: Thanks, it worked! On Dec 28, 6:47 pm, Michael Bayer [EMAIL PROTECTED] wrote: actually the GROUP BY wont go through as a keyword argument to query.select() at the moment, so youd have to use the full select statement. but also, i dont see why youd want that GROUP BY in the query, you dont have any aggregate functions in your column list and also GROUP BY requires all non-aggregates to be part of the group (at least in postgres). In MySQL and SQLite the GROUP BY removes the duplicate entries. (For every post in the topic there is a row of that topic in the result.) --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---