[sqlalchemy] Re: SA cascade conflicts with MySQL ON DELETE
Conor, Thanks for your help! On Tue, Oct 20, 2009 at 16:27, Conor conor.edward.da...@gmail.com wrote: You can tell SQLAlchemy that the database will cascade deletes on its own (add passive_deletes=True to the relation arguments, see http://www.sqlalchemy.org/docs/05/mappers.html#using-passive-deletes for more info). However, I was under the impression that even with passive_deletes disabled, SQLAlchemy would know to delete child objects before parent objects, so it should have worked anyway. It turns out passive_deletes=True did help. Though a bit weird. I get the same ConcurrentModificationError when I added i to Community mapper, like this: mapper(Community, communities_tbl, properties={ 'members':relation(CommunityMember, backref='community', passive_deletes=True, cascade=all, delete, delete-orphan), 'feeds':relation(CommunityFeed, backref='community', passive_deletes=True, cascade=all, delete, delete-orphan), }) Yet, when just out of desperation I added it to CommunityFeed mapper it started working. The mapper did have cascade spec though: mapper(CommunityFeed, community_feeds_tbl, properties={ 'user':relation(User, backref='feeds'), 'entries':relation(PlanetEntry, backref='feed', passive_deletes=True, cascade=all, delete, delete-orphan), }) -- Max.Ischenko // developers.ua // vse-sto.com.ua Follow me on Twitter, twitter.com/maxua --~--~-~--~~~---~--~~ 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] SA cascade conflicts with MySQL ON DELETE
I have three related tables in MySQL and a corresponding mappings in SQLAlchemy. It all works fine until I tried to delete objects. I tinkered with different options but couldn't get it to work. Here is the relevant mappings: mapper(PlanetEntry, community_planet_tbl, properties={ }, order_by=[community_planet_tbl.c.updated.desc()]) mapper(CommunityFeed, community_feeds_tbl, properties={ 'user':relation(User, backref='feeds'), 'entries':relation(PlanetEntry, backref='feed', cascade=all, delete, delete-orphan), }) mapper(CommunityMember, community_members_tbl) mapper(Community, communities_tbl, properties={ 'members':relation(CommunityMember, backref='community', cascade=all, delete, delete-orphan), 'feeds':relation(CommunityFeed, backref='community', cascade=all, delete, delete-orphan), }) I am attempting to delete a row from communities table (instance of Communy entity) and I get the following error: File '/home/max/projects/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.6-py2.5.egg/sqlalchemy/orm/unitofwork.py', line 762 in delete_objects task.mapper._delete_obj(task.polymorphic_todelete_objects, trans) File '/home/max/projects/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.6-py2.5.egg/sqlalchemy/orm/mapper.py', line 1527 in _delete_obj number of objects deleted %d % (c.rowcount, len(del_objects))) ConcurrentModificationError: Deleted rowcount 0 does not match number of objects deleted 1 Introspection shows that it tries to delete CommunityMember objects with DELETE FROM community_members WHERE community_members.community_id = %s AND community_members.member_id = %s I suspect these are already gone since there is a CASCADE rule in community_members table in MySQL: FOREIGN KEY (`community_id`) REFERENCES `communities` (`id`) ON DELETE CASCADE ON UPDATE CASCADE. Why SQLAlchemy does not grok this? If I change mapper to the following (remove cascade spec): mapper(Community, communities_tbl, properties={ 'members':relation(CommunityMember, backref='community'), I get this: File '/home/max/projects/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.6-py2.5.egg/sqlalchemy/orm/sync.py', line 28 in clear raise AssertionError(Dependency rule tried to blank-out primary key column '%s' on instance '%s' % (r, mapperutil.state_str(dest))) AssertionError: Dependency rule tried to blank-out primary key column 'community_members.community_id' on instance 'CommunityMember at 0x31a0450' Looks correct to me. Why the original cascade clause does not work then? Insight is much appreciated. Max. --~--~-~--~~~---~--~~ 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] orm querying: where with a subquery on self
I'm trying to express the following SQL: SELECT * FROM attendances a WHERE grade = (SELECT MAX(grade) FROM attendances WHERE student_id=a.student_id) and school_id=112; A2 = aliased(A) # A is Attendance class q2 = s.query(max_grade).filter(A.student_id==A2.student_id).subquery() print q2 # looks OK so far SELECT max(attendances.grade) AS max_1 FROM attendances, attendances AS attendances_1 WHERE attendances.student_id = attendances_1.student_id print s.query(A).filter(and_(A.grade==q2, A.school_id==112)) SELECT attendances.id AS attendances_id, attendances.student_id AS attendances_student_id, attendances.school_id AS attendances_school_id, attendances.school_year_id AS attendances_school_year_id, attendances.grade AS attendances_grade FROM attendances, (SELECT max(attendances.grade) AS max_1 FROM attendances, attendances AS attendances_1 WHERE attendances.student_id = attendances_1.student_id) AS anon_1 WHERE attendances.grade = SELECT max(attendances.grade) AS max_1 FROM attendances, attendances AS attendances_1 WHERE attendances.student_id = attendances_1.student_id AND attendances.school_id = %(school_id_1)s # doesn't work: LINE 8: WHERE attendances.grade = SELECT max(attendances.grade) AS m... If I do not use .subquery() it doesn't do correct thing either: q3 = s.query(max_grade).filter(A.student_id==A2.student_id) print s.query(A).filter(and_(A.grade==q3, A.school_id==112)) SELECT attendances.id AS attendances_id, attendances.student_id AS attendances_student_id, attendances.school_id AS attendances_school_id, attendances.school_year_id AS attendances_school_year_id, attendances.grade AS attendances_grade FROM attendances WHERE attendances.grade = %(grade_1)s AND attendances.school_id = % (school_id_1)s Pls help, Max. --~--~-~--~~~---~--~~ 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 querying: where with a subquery on self
On 12 май, 10:17, Max Ischenko ische...@gmail.com wrote: I'm trying to express the following SQL: SELECT * FROM attendances a WHERE grade = (SELECT MAX(grade) FROM attendances WHERE student_id=a.student_id) and school_id=112; I've got it working using literal SQL but there must be a better way? print s.query(A).filter(and_(A.school_id==112, grade = (SELECT MAX(grade) FROM attendances a WHERE attendances.student_id=a.student_id))) SELECT attendances.id AS attendances_id, attendances.student_id AS attendances_student_id, attendances.school_id AS attendances_school_id, attendances.school_year_id AS attendances_school_year_id, attendances.grade AS attendances_grade FROM attendances WHERE attendances.school_id = %(school_id_1)s AND grade = (SELECT MAX (grade) FROM attendances a WHERE attendances.student_id=a.student_id) Max. --~--~-~--~~~---~--~~ 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 querying: where with a subquery on self
On Tue, May 12, 2009 at 11:43, a...@svilendobrev.com wrote: try label the column in q2, say q2.maxgrade, then use that as print s.query(A).filter( A.grade==q2.maxgrade)... Doesn't work: q2 = s.query(max_grade.label('maxgrade')).filter(A.student_id==A2.student_id).subquery() print s.query(A).filter(and_(A.school_id==112, A.grade==q2.maxgrade)) Traceback (most recent call last): File console, line 1, in module AttributeError: 'Alias' object has no attribute 'maxgrade' --~--~-~--~~~---~--~~ 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: how to .join() two many-to-many relations?
On Sun, Apr 12, 2009 at 19:21, Michael Bayer mike...@zzzcomputing.comwrote: use a seperate join() call for each path. join(path1, path2, path3 ...) assumes thats one path along related entities. Thanks! Just in case anyone interested, here is final (working) code: clauses = [] if skill: q = q.join('profile', 'skills') clauses.append(Skill.name == skill) if city: q = q.join('profile', 'city') clauses.append(City.name == city) members = q.filter(and_(*clauses)) -- Max.Ischenko // twitter.com/maxua --~--~-~--~~~---~--~~ 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] how to .join() two many-to-many relations?
Hi, SQLAlchemy magic is needed! I have users table plus two many-to-many relations (skills and cities). I'm trying to implement a search on one or two of these relations. It works fine if I join with skills OR cities but it gives weird error when I'm trying to join both. I suspect my .join() call just incorrect, I'd be very grateful if someone could point out what's wrong with it. Here is the code. relations = ['profile'] if skill: relations.append('skills') if city: relations.append('city') q = self.db.query_users.join(*relations) if skill: q = q.filter(Skill.name == skill) if city: q = q.filter(City.name == city) And the traceback I get when both skill and city are non-empty: File '/var/www/dou-www/doupy/doupy/controllers/members.py', line 263 in browse q = self.db.query_users.join(*relations) File 'string', line 1 in lambda File '/var/www/dou-www/py/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/util.py', line 212 in go return fn(*args, **kw) File '/var/www/dou-www/py/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py', line 873 in join return self.__join(props, outerjoin=False, create_aliases=aliased, from_joinpoint=from_joinpoint) File 'string', line 1 in lambda File '/var/www/dou-www/py/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py', line 52 in generate fn(self, *args[1:], **kw) File '/var/www/dou-www/py/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py', line 956 in __join descriptor, prop = _entity_descriptor(left_entity, onclause) File '/var/www/dou-www/py/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/util.py', line 508 in _entity_descriptor desc = entity.class_manager[key] KeyError: 'city' -- Max.Ischenko // twitter.com/maxua --~--~-~--~~~---~--~~ 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: text binding for sql queries
On 13 фев, 18:46, a...@svilendobrev.com wrote: i guess s.execute(stmt, params=dict(codeword=codeword) ) It worked, thanks a lot! Strangely though params= not mentioned on this page: http://www.sqlalchemy.org/docs/05/sqlexpression.html Max. --~--~-~--~~~---~--~~ 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] get() can only be used against a single mapped class
Hi, I do a simple query and got very strange error: File '/home/max/projects/site-baseline/doupy/doupy/controllers/ salarydb.py', line 432 in record c.user = s.query(WordpressUser).get(c.rec.user_id) File '/home/max/projects/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/query.py', line 446 in get key = self._only_mapper_zero(get() can only be used against a single mapped class.).identity_key_from_primary_key(ident) File '/home/max/projects/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/mapper.py', line 1048 in identity_key_from_primary_key return (self._identity_class, tuple(util.to_list(primary_key))) TypeError: 'NoneType' object is not iterable The stange thing is that it only occurs if there is no matching record in the database. If there is a user with given id it works fine. Is it a bug or what? --~--~-~--~~~---~--~~ 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] text binding for sql queries
I get an error when I'm trying tu run this: stmt = text(select * from salary_data_new where codeword=:codeword union select * from salary_data_clean where codeword=:codeword ) # s is instance of Session() class factory c.rec = s.execute(stmt, codeword=codeword).fetchone() I get: File '/home/max/projects/site-baseline/doupy/doupy/controllers/salarydb.py', line 420 in record c.rec = s.execute(stmt, codeword=codeword).fetchone() File '/home/max/projects/site-baseline/py/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalchemy/orm/session.py', line 752 in execute engine = self.get_bind(mapper, clause=clause, **kw) TypeError: get_bind() got an unexpected keyword argument 'codeword' What am I doing wrong? -- Max.Ischenko http://www.developers.org.ua The #1 Ukrainian Software Developers' site News, Companies, Jobs, Events, Talks and more. --~--~-~--~~~---~--~~ 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: save on new objects?
Just a follow up to let you know SA 0.5 now runs smoothly on my site. Actually, it has been running for about a week now. Onto your comments: I didn't use get_session() but I did use session.mapper instead of normal orm.mapper class. The reason for this is here http://tinyurl.com/2a76hp - I was getting exception about Parent not bound. Since site was (hastly) put in production and, worse of all, I couldn't reproduce the problem locally. Hence I tried several things at once. Something helped, though I can't tell what exactly. Alas. Here are the changes I did: 1. Use the same Session() instance through the request environ['sqlalchemy_session'] = sess = Session() (I used to use just Session class as is, without instantiating 2. Switched to sqlalchemy.orm.mapper and removed Session.extension = Session.extension.configure(save_on_init=False) I added a bit earlier My guess is the #1 was the root cause, though there are still a few places which just use Session (they are harder to refactor) yet it works fine. Max. you definitely need to get that code out of production until you work out your session usage. It seems likely that your app is relying on a certain behavior that we removed in 0.5 which is the get_session() method on Mapper.the 05Migration doc has a note about this which is probably easy to miss: get_session() - this method was not very noticeable, but had the effect of associating lazy loads with a particular session even if the parent object was entirely detached, when an extension such as scoped_session() or the old SessionContextExt was used. It's possible that some applications which relied upon this behavior will no longer work as expected; but the better programming practice here is to always ensure objects are present within sessions if database access from their attributes are required. You're the first person that has reported running into this issue in the 05 series so it seemed hopeful that the removal of this method wasn't going to have a big impact. A common pattern which can cause what you see there is if your templates are accessing lazy loaders on objects, but the Session which loaded those objects had been clear()'ed or otherwise closed before the template was allowed to render. You should try to keep the same Session open throughout the full request lifecycle, and you should avoid removing any objects from the Session which you wish to continue using. Alternatively, this kind of error may occur if you are using detached objects that are stored in a persistent cache. These objects wont auto attach when used for the same reason, and in fact even in the 0.4 series this is a dangerous pattern since globally accessible objects can be accessed by multiple threads. If you want to bring cached objects into a current session, bring them in using session.merge(dont_load=True). There's a recipe illustrating this in the distribution in examples/query_caching/query_caching.py if you want to see a working pattern. -- Max. http://www.developers.org.ua/m/max/ --~--~-~--~~~---~--~~ 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] setting up insert many stmt?
Hello, I'm having trouble doing insertmany correctly. I read the docs and came up with this code: s = Session() ins = data_tbl.insert() records = [] for rec in s.execute(sql).fetchall(): records.append(rec) ins.execute(records) It gives an error: File '/var/www/site-baseline/doupy/doupy/controllers/salarydb.py', line 170 in update_clean ins.execute(records) File '/var/www/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.0rc4-py2.5.egg/sqlalchemy/sql/expression.py', line 1129 in execute return e.execute_clauseelement(self, multiparams, params) File '/var/www/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.0rc4-py2.5.egg/sqlalchemy/engine/base.py', line 1181 in execute_clauseelement return connection.execute_clauseelement(elem, multiparams, params) File '/var/www/site-baseline/py/lib/python2.5/site-packages/ SQLAlchemy-0.5.0rc4-py2.5.egg/sqlalchemy/engine/base.py', line 848 in execute_clauseelement keys = params[0].keys() AttributeError: 'list' object has no attribute 'keys' Huh? Max. --~--~-~--~~~---~--~~ 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: mapping of Wordpress taxonomy tables
On 5 май, 11:55, Max Ischenko [EMAIL PROTECTED] wrote: Hello, My python app uses Wordpress blog database and my sqlalchemy mappings recently broke due to Wordpress update. Can someone pls help to map the following taxonomy tables into SA? I've tried to follow Specifying Alternate Join Conditions to relation() recipe from the docs and got an error about invalid kwarg: File /usr/lib/python2.5/site-packages/SQLAlchemy-0.4.5-py2.5.egg/ sqlalchemy/orm/__init__.py, line 566, in mapper return Mapper(class_, local_table, *args, **params) TypeError: __init__() got an unexpected keyword argument 'foreign_keys' Here is the offending code: mapper(WordpressPost, wp_posts_tbl, properties={ 'categories': relation(WordpressTaxonomy, primaryjoin= and_(wp_terms_taxonomy_tbl.c.taxonomy=='category', wp_term_relationships_tbl.c.term_taxonomy_id==wp_terms_taxonomy_tbl.c.term_taxonomy_id)), }, foreign_keys=[wp_terms_taxonomy_tbl.c.term_taxonomy_id]) Max. --~--~-~--~~~---~--~~ 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: mapping of Wordpress taxonomy tables
On 5/6/08, Michael Bayer [EMAIL PROTECTED] wrote: mapper(WordpressPost, wp_posts_tbl, properties={ 'categories': relation(WordpressTaxonomy, primaryjoin= and_(wp_terms_taxonomy_tbl.c.taxonomy=='category', wp_term_relationships_tbl .c.term_taxonomy_id==wp_terms_taxonomy_tbl.c.term_taxonomy_id)), }, foreign_keys=[wp_terms_taxonomy_tbl.c.term_taxonomy_id]) foreign_keys goes inside of relation(). Thanks. Then I think this is a typo here: http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_relation_customjoin_fks Max. --~--~-~--~~~---~--~~ 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] mapping of Wordpress taxonomy tables
Hello, My python app uses Wordpress blog database and my sqlalchemy mappings recently broke due to Wordpress update. Can someone pls help to map the following taxonomy tables into SA? http://codex.wordpress.org/WordPress_Taxonomy I kind of stuck with sqlalchemy.exceptions.ArgumentError: Error determining primary and/or secondary join for relationship 'WordpressPost.terms (WordpressTerm)'. If the underlying error cannot be corrected, you should specify the 'primaryjoin' (and 'secondaryjoin', if there is an association table present) keyword arguments to the relation() function (or for backrefs, by specifying the backref using the backref() function with keyword arguments) to explicitly specify the join conditions. Nested error is Can't find any foreign key relationships between 'wp_terms' and 'wp_terms' --~--~-~--~~~---~--~~ 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] can't setup simple one to many relation
Hello, I'm struggling with a simple one to many relation, pls help! active_meetup = MeetupEvent(...) reg = MeetupRegistration(meeting=active_meetup) db.save(reg) *class 'sqlalchemy.exceptions.OperationalError': (OperationalError) (1048, Column 'meeting_id' cannot be null) u'INSERT INTO dou_meetup_registrants (meeting_id, name, email, phone) VALUES (%s, %s, %s, %s)' [None, u'1.0', u'[EMAIL PROTECTED]', u'+38 063 2061046']* Obviously SA somehow can't find meeting_id even thou reg.meeting is OK. Here is my mapping code: dou_meetupregs_tbl = Table('dou_meetup_registrants', meta, autoload=True) dou_meetups_tbl = Table('dou_meetups', meta, autoload=True) mapper(MeetupEvent, dou_meetups_tbl, properties={ 'registrants':relation(MeetupRegistration, backref='meetup'), }) mapper(MeetupRegistration, dou_meetupregs_tbl, properties={ }) And classes (just in case): class MeetupEvent(object): regcount = property(fget=lambda self: len(self.registrants)) def __repr__(self): return MeetupEvent %s %r % (self.id, self.url) class MeetupRegistration(object): def __init__(self, meeting, name=None, email=None, phone=None): if not meeting: raise ValueError(Meeting event not set) if not email: raise ValueError(Email is required) self.meeting = meeting self.name = name self.email = email self.phone = phone def __repr__(self): return MeetupRegistration %s (%s,%s) % (self.id, self.meeting.id, self.email) -- Max http://maxischenko.in.ua // http://www.linkedin.com/in/maksim --~--~-~--~~~---~--~~ 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: can't setup simple one to many relation
On 29 янв, 10:47, Alexandre Conrad [EMAIL PROTECTED] wrote: Hello Max, AFAIR, I've had similar problems latelty when having a table with composite primary keys and running SQLite. I think Mike told me SQLite didn't support that. I'm no SQLite user, but I had this problem when I had to make a portable test case with SQLite in memory. If this can put you on track. Well, I'm using MySQL 5. Table defs: DROP TABLE IF EXISTS `dou_meetup_registrants`; CREATE TABLE `dou_meetup_registrants` ( `id` int(11) NOT NULL auto_increment, `meeting_id` varchar(20) NOT NULL default '', `name` varchar(80) default NULL, `email` varchar(80) NOT NULL default '', `phone` varchar(24) default NULL, PRIMARY KEY (`id`), UNIQUE KEY `meeting_id` (`meeting_id`,`email`), CONSTRAINT `dou_meetup_registrants_ibfk_1` FOREIGN KEY (`meeting_id`) REFERENCES `dou_meetups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; CREATE TABLE `dou_meetups` ( `id` varchar(16) NOT NULL, `url` varchar(255) NOT NULL, `created` timestamp NOT NULL default CURRENT_TIMESTAMP, `is_active` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`), UNIQUE KEY `url` (`url`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; SQLAlchemy 0.4.2 --~--~-~--~~~---~--~~ 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: can't setup simple one to many relation
On Jan 29, 2008 3:46 PM, jason kirtland [EMAIL PROTECTED] wrote: The mapped backref is 'meetup' but the class's __init__ is setting 'meeting': Duh! Thanks a lot. Max. --~--~-~--~~~---~--~~ 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: two scoped sessions in one app?
I had a Session.configure() statement which was called for both sessions and this was making setup unusable. Duh. On Jan 16, 2008 2:12 PM, Max Ischenko [EMAIL PROTECTED] wrote: On Jan 15, 2008 6:14 PM, Michael Bayer [EMAIL PROTECTED] wrote: if you are using multiple scoped sessions you won't be able to use Session.mapper - the idea of Session.mapper is that all instances get tied to a single contextual session. OK, so how do I set it up. I'm reading http://www.sqlalchemy.org/docs/04/session.html but answer isn't clear. Should I use set up one scope session and another with create_session? -- Max http://maxischenko.in.ua // http://www.linkedin.com/in/maksim --~--~-~--~~~---~--~~ 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: two scoped sessions in one app?
On Jan 15, 2008 6:14 PM, Michael Bayer [EMAIL PROTECTED] wrote: if you are using multiple scoped sessions you won't be able to use Session.mapper - the idea of Session.mapper is that all instances get tied to a single contextual session. OK, so how do I set it up. I'm reading http://www.sqlalchemy.org/docs/04/session.html but answer isn't clear. Should I use set up one scope session and another with create_session? --~--~-~--~~~---~--~~ 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: two scoped sessions in one app?
Hello, Sorry for reply to myself; just want to tell that the problem is solved. Here is how my setup looks like: Session = scoped_session(sessionmaker(autoflush=True, transactional=False)) SessionCDB = scoped_session(sessionmaker(autoflush=True, transactional=False)) ... # application startup sqlalchemy_configure(blog, Session, init_blog_meta) sqlalchemy_configure(cdb, SessionCDB, init_companydb_meta) ... def sqlalchemy_configure(dbname, session, callback): global metadata_cache from sqlalchemy import engine_from_config from pylons import config engine = engine_from_config(config, 'sqlalchemy.%s.' % dbname) session.configure(bind=engine) meta = sqlalchemy.MetaData(engine) metadata_cache[dbname] = meta callback(session.mapper, meta) Max. --~--~-~--~~~---~--~~ 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: migrating to 0.4: Parent instance is not bound
Hello Paul, On 8 янв, 17:07, Paul Johnston [EMAIL PROTECTED] wrote: Not 100% sure without seeing your model, but you probably want to use session.mapper in place of mapper. That was it, thanks a lot! Max. --~--~-~--~~~---~--~~ 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: migrating to 0.4: Parent instance is not bound
Hello, My migration to 0.4 didn't end well since now I sometimes get the following error: sqlalchemy.exceptions.InvalidRequestError: Parent instance class ' doupy.model.objects.JobPosting' is not bound to a Session, and no contextual session is established; lazy load operation of attribute 'author' cannot proceed. My SA setup code looks like this: # module globals Session = scoped_session(sessionmaker(autoflush=True, transactional=False)) metadata_cache = {} # this is called only once, when application starts engine = engine_from_config(...) Session.configure(bind=engine) # when processing web requests Session.query(..) Max. * * --~--~-~--~~~---~--~~ 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: migrating to 0.4: session handling
Hello Michael, Thanks for your enlightening comments, pls see my comments below. On 12/29/07, Michael Bayer [EMAIL PROTECTED] wrote: the transaction commit inside the create_draft method is a little strange. usually, if you have a long running transaction, there would be a begin()/commit() pair framing a series of operations. issuing commits inside of method sets seems to imply you dont really want a transactional sessionbut the session begins the next transaction lazily so theres probably nothing terrible about doing it that way. Got it, I think. I get rid of all .commit() calls and wrap all methods with explicit transactions: @transactional_method() def create_draft(self): # ... def transactional_method(): def wrapper(func, self, *args, **kwargs): session = self.session # shortcut session.begin() try: rv = func(self, *args, **kwargs) session.commit() return rv except: session.rollback() raise return decorator(wrapper) I also turn off automatic transactions: Session = scoped_session(sessionmaker(autoflush=True, transactional=False)) Does it looks correct? It works, as far as my tests tell me. I've tried transactional=True but it broke the app in a lot of places where there were no explicit commits. May be I update the code eventually to always be explicit but for now I'm more concerned to get the code working again, after 0.4 update. Btw, is it possible to retrieve metadata if you have an engine or configured session object? I haven't found a way so ended up storing it in a module global when session is configured. MetaData is usually a module global, its attached to tables alsoso for example class_mapper(AnyMappedClass).mapped_table.metadata This method works for me, thanks. Max. --~--~-~--~~~---~--~~ 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] migrating to 0.4: session handling
Hello, I am porting my code to SA 0.4 and cannot figure out whether or not I should work correctly. I have most of my db-related code united under a single DatabaseFacade class which is then bound to SA session via property: class DatabaseFacade(object): ... session = property(fget=lambda self: Session()) Session is setup as: Session = scoped_session(sessionmaker(autoflush=True, transactional=True)) and configured later on. Here is how I use it: def create_draft(self, **kw): p = WordpressPost(**kw) self.session.save(p) self.session.commit() return p Since self.session is a property it calls Session() repeatedly. It seems to work but is it OK, from transactional/performance point of view? Do I need to change it to something like: s = self.session # obtain new session s.save(p) s.commit() I also have a transactional_method() decorator which does session().begin() and then commit() or rollback() depending on whether exception occured or not. I also noticed that session.save() fails if I try to save a persistent object so I am forced to change every such save() call to save_or_update(). I don't mind but why it's not mentioned in whatsnew40/migration guide? Another error I am now getting is: InvalidRequestError: Instance '[EMAIL PROTECTED]' is with key (class ' doupy.model.objects.Invoice', (73L,), None) already persisted with a different identity Any ideas how to fix this? Method impl. looks lke this (edited for brevity): @transactional_method() def create_invoice(self, wpuser, **kw): invoice = Invoice(wpuser, public_id=str(next_id), **kw) self.session.save(invoice) return invoice Btw, is it possible to retrieve metadata if you have an engine or configured session object? I haven't found a way so ended up storing it in a module global when session is configured. -- Max http://maxischenko.in.ua // http://www.linkedin.com/in/maksim --~--~-~--~~~---~--~~ 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: migrating to 0.4: session handling
On 28 дек, 11:20, Max Ischenko [EMAIL PROTECTED] wrote: Another error I am now getting is: InvalidRequestError: Instance '[EMAIL PROTECTED]' is with key (class ' doupy.model.objects.Invoice', (73L,), None) already persisted with a different identity Any ideas how to fix this? Method impl. looks lke this (edited for brevity): @transactional_method() def create_invoice(self, wpuser, **kw): invoice = Invoice(wpuser, public_id=str(next_id), **kw) self.session.save(invoice) return invoice Randomly put db.session.clear() before create_invoice() call fixed it. Black magic. ;-/ --~--~-~--~~~---~--~~ 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: backref error when migrated to 0.4
Hello Bertrand, It works like a charm, thanks a lot. Max. On 12/27/07, Bertrand Croq [EMAIL PROTECTED] wrote: JobPosting's mapper tells RefdataLocation's mapper to add a property named 'vacancies', then you tell RefdataLocation's mapper to add a property named 'vacancies'. Replace these 2 lines by mapper( JobPosting, jobad_posts_tbl, properties = { 'jb_location': relation( RefdataLocation, backref = backref( 'vacancies', order_by = desc(jobad_posts_tbl.c.published_date) ) ), } ) and it should work as expected. -- Bertrand Croq ___ Net-ng Tel : +33 (0)223 21 21 53 14, rue Patis Tatelin Fax : +33 (0)223 21 21 60 Bâtiment G Web : http://www.net-ng.com 35000 RENNESe-mail: [EMAIL PROTECTED] FRANCE -- Max http://maxischenko.in.ua // http://www.linkedin.com/in/maksim --~--~-~--~~~---~--~~ 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] querying many-to-many (WordPress)
Hello, I'm using SQLAlchemy to access my WordPress database and I need to query posts from particular category. There is a many-to-many mapping between wp_posts and wp_categories table, throught wp_post2cat table. I was able to come up with the following code: cats = self.meta.tables['wp_categories'] posts = self.meta.tables['wp_posts'] post2cat = self.meta.tables['wp_post2cat'] q = self.session.query(WordpressPost) q = q.filter(WordpressPost.c.status=='publish') q = q.filter(WordpressCategory.c.slug=='sitenews') q = q.filter(post2cat.c.post_id==posts.c.ID) q = q.filter(post2cat.c.category_id==cats.c.cat_ID) It works correctly but seems too verbose to me. Basically I do two joins by hand. I suspect SQLAlchemy can do this for me, just can't figure out how. Help, please? Max. P.S.: I have: mapper(WordpressCategory, wp_categories_tbl , properties={ 'id' : wp_categories_tbl.c.cat_ID, ... }) mapper(WordpressPost, wp_posts_tbl, properties={ 'id' : wp_posts_tbl.c.ID, ... 'categories': relation(WordpressCategory, secondary=wp_post2cat_tbl), }) --~--~-~--~~~---~--~~ 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] Strange UnicodeDecodeError in SA 0.3.7
Hello, After recent upgrade of my Pylons app I have been seeing the errors like this one: sqlalchemy.exceptions.SQLError: (UnicodeDecodeError) 'utf8' codec can't decode byte 0x96 in position 718: unexpected code byte u'SELECT jobad_posts.post_status AS jobad_posts_post_status, jobad_posts.post_title AS jobad_posts_post_title, jobad_posts.post_content AS jobad_posts_post_content, jobad_posts.last_modified AS jobad_posts_last_modified, jobad_posts.published_date AS jobad_posts_published_date, jobad_posts.duration AS jobad_posts_duration, jobad_posts.post_author AS jobad_posts_post_author, jobad_posts.`ID` AS `jobad_posts_ID`, jobad_posts.expires_on AS jobad_posts_expires_on \nFROM jobad_posts \nWHERE (jobad_posts.post_status = %s AND jobad_posts.expires_on = %s) ORDER BY jobad_posts.published_date DESC' ['publish', datetime.datetime(2007, 6, 7, 16, 35, 1, 409021)] The strange thing is how they occur. For some time after application starts (if may be a few days) it runs these queries just fine but after awhile all queries fail with the exception like above. If application is restarted it runs fine again. Any ideas? Btw, here is the traceback I see: Module doupy.models:152 in select_active_job_postings now = datetime.datetime.now() return self.select_some_job_postings(expdate=now, limit=limit, show_expired=False, post_status='publish') def select_some_job_postings(self, expdate=None, limit=limit, show_expired=False, post_status='publish') Module doupy.models:200 in select_some_job_postings if limit: posts = posts[:limit] return posts.list() def auth_user_by_credentials(self, username, password): return posts.list() Module sqlalchemy.orm.query:809 in list Module sqlalchemy.orm.query:818 in __iter__ Module sqlalchemy.orm.query:326 in select_whereclause Module sqlalchemy.orm.query:927 in _select_statement Module sqlalchemy.orm.query:831 in execute Module sqlalchemy.orm.session:183 in execute Module sqlalchemy.engine.base:509 in execute Module sqlalchemy.engine.base:549 in execute_clauseelement Module sqlalchemy.engine.base:560 in execute_compiled Module sqlalchemy.engine.base:573 in _execute_raw Module sqlalchemy.engine.base:591 in _execute Max. --~--~-~--~~~---~--~~ 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] auto-load ForeignKey references?
Hi, If I have two tables related via foreign key how can I tell SA that accessing foreign key should fetch related object automatically? By default it simply gives me the FK as integer which is not what I want. Here are my mappers: wp_users_tbl = Table('wp_users', meta, autoload=True) wp_posts_tbl = Table('wp_posts', meta, Column('post_author', Integer, ForeignKey(wp_users_tbl.c.ID)), autoload=True) mapper(WordpressPost, wp_posts_tbl) mapper(WordpressUser, wp_users_tbl, properties={ 'posts' : relation(WordpressPost), }) post = db.select_one(...) print post.post_author # prints 123 instead of WordpressUser instance Thanks, Max. --~--~-~--~~~---~--~~ 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: auto-load ForeignKey references?
Hello, On May 10, 4:38 pm, King Simon-NFHD78 [EMAIL PROTECTED] wrote: You're halfway there with your 'posts' relation. I think if you pass backref='author' in your relation, then WordpressPost objects will get an 'author' property which points back to the WordpressUser. Nope, it doesn't work. At least, I can't get it to work. If I use backref='author' new attribute 'author' appears but equals None even though the author_id is something like 123. Max. --~--~-~--~~~---~--~~ 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: auto-load ForeignKey references?
Hi, On 5/10/07, King Simon-NFHD78 [EMAIL PROTECTED] wrote: Nope, it doesn't work. At least, I can't get it to work. If I use backref='author' new attribute 'author' appears but equals None even though the author_id is something like 123. You're not getting caught by this, are you: http://www.sqlalchemy.org/trac/wiki/WhyDontForeignKeysLoadData Basically, setting author_id to a number won't automatically cause the author to be loaded. If that's not the case in your situation, I'm out of ideas. Do you have a test case? Actually, it works. More precisely, I was getting None because the corresponding entry in wp_users was missing (I love this ref. integrity in MySQL). If post_author was valid then FK was loaded (if backref were specified). Thanks for helping me out. Max. --~--~-~--~~~---~--~~ 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] working with multiple databases
Hello, I'm struggling to setup SA/Pylons for a multidatabase env without much luck. As far as I understand, I need a session per database. In Pylons, I get it for free via session_context binding. In other words, I have setup a session_context object for each of the database I need to work with. db.get_db_session('blog') sqlalchemy.orm.session.Session object at 0xb7337a8c db.get_db_session('cdb') sqlalchemy.orm.session.Session object at 0xb720f7ec Nevertheless, it is not usable: model.blog_wp_posts_tbl.count().execute() Traceback (most recent call last): File console, line 1, in ? File build/bdist.linux-i686/egg/sqlalchemy/sql.py, line 474, in execute File build/bdist.linux-i686/egg/sqlalchemy/sql.py, line 411, in execute except Exception ,e: InvalidRequestError: This Compiled object is not bound to any engine. I suppose my metadata setup is wrong. Currently it looks like this: blog_meta = DynamicMetaData() blog_wp_posts_tbl = Table('wp_posts', blog_meta, Column('ID', Integer, primary_key=True), Column('guid', String(255)), Column('post_author', Integer, ForeignKey('wp_users.ID'), nullable=False), Column('post_content', Unicode), ) Do I need to use separate DynamicMetaData() for each db conn? Do I need to connect single metadata to the correct session? SA docs shows how to connect metadata to a engine but all I have is a session. Max. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---