[sqlalchemy] Newbie (yeah, me) has some problems adding an object to the database
Dear list I'm new to sqlalchemy and so far, I am very impressed. I was able to install SQLalchemy in about two minutes or so :-) and at the moment, I am trying to follow the steps in the documentation. I started with three tables (shown below) and tried to save a user in the database using the information in http://www.sqlalchemy.org/docs/05/ormtutorial.html#adding-new-objects I'm getting the following error caused by the last line in my script: localhost:squirrel michael$ python squirrel.py Traceback (most recent call last): File squirrel.py, line 47, in module our_user = session.query(User).filter_by(login='ed').first() File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/query.py, line 909, in first ret = list(self[0:1]) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/query.py, line 937, in __iter__ self.session._autoflush() File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/session.py, line 771, in _autoflush self.flush() File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/session.py, line 789, in flush self.uow.flush(self, objects) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/unitofwork.py, line 237, in flush flush_context.execute() File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/unitofwork.py, line 449, in execute UOWExecutor().execute(self, tasks) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/unitofwork.py, line 934, in execute self.execute_save_steps(trans, task) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/unitofwork.py, line 949, in execute_save_steps self.save_objects(trans, task) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/unitofwork.py, line 940, in save_objects task.mapper._save_obj(task.polymorphic_tosave_objects, trans) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/mapper.py, line 1008, in _save_obj connection = uowtransaction.transaction.connection(self) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/session.py, line 178, in connection return self.get_or_add(engine) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/session.py, line 208, in get_or_add conn = self._parent.get_or_add(bind) File /Library/Python/2.5/site-packages/SQLAlchemy-0.4.8-py2.5.egg/ sqlalchemy/orm/session.py, line 217, in get_or_add conn = bind.contextual_connect() AttributeError: 'MetaData' object has no attribute 'contextual_connect' Could anyone tell me why this is happening? Here's my complete code I've used: from sqlalchemy import * from datetime import datetime import sys metadata = MetaData('sqlite:///squirrel.db') user_table = Table( 'user', metadata, Column('id', Integer, primary_key=True), Column('login', Unicode(25), nullable=False), Column('password', Unicode(40), nullable=False) ) person_table = Table( 'person', metadata, Column('id', Integer, primary_key=True), Column('firstname', Unicode(25)), Column('lastname', Unicode(25)), Column('birthday', DateTime) ) person_user = Table( 'person_user', metadata, Column('id', Integer, primary_key=True), Column('person_id', Integer, ForeignKey('person.id')) ) metadata.create_all() class User(object): def __init__(self, login, password): self.login = login self.password = password def __repr__(self): return User('%s', '%s') % (self.login, self.password) from sqlalchemy.orm import mapper mapper(User, user_table) from sqlalchemy.orm import sessionmaker Session = sessionmaker(bind=metadata) session = Session() ed_user = User('ed', 'password of ed') session.add(ed_user) our_user = session.query(User).filter_by(login='ed').first() --~--~-~--~~~---~--~~ 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: Newbie (yeah, me) has some problems adding an object to the database
never mind, found the mistake. should have read it from the beginning :-/ --~--~-~--~~~---~--~~ 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] Odd inheritance issue
Hi All, I have something stuping me right now. I'm using SqlAlchemy . 0.5.2 and Python 2.6. My issue is as follows: This class works perfectly: import sys import pprint import pdb from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import * sys.path.append('../config') import config from MemberProfiles import * from Gender import * class MemberInfo: def __init__(self): engine = create_engine(config.db_conn) Session = sessionmaker(bind=engine) self.session = Session() Base = declarative_base() metdata = Base.metadata def GET(self,memberid): memberInfo = self.session.query(MemberProfile,Gender).filter (MemberProfile.memberID==memberid).first() print memberInfo[0].__dict__,memberInfo[1].__dict__ return memberInfo[0] if __name__ == __main__: x=MemberInfo() x.GET(81017) This class creates in instance of the above class, and the invocation of the above class returns an empty result: import sys import cherrypy import routes import pdb sys.path.append('../models') import MemberInfo class Dispatch: def __init__(self): self.methods = ('OPTIONS','GET','HEAD','POST', 'PUT','DELETE','TRACE','CONNECT') self.mp = MemberInfo.MemberInfo() def test(self): #http_method = getattr(self,self.mp.%s % cherrypy.request.method) result=self.mp.GET(18087) return result.memberID if __name__ == __main__: x=Dispatch() print x.test() They're both using the same confi file to open the same database. I am really boggled about this. What am I missing? Thank you in advance, Gloria --~--~-~--~~~---~--~~ 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: Odd inheritance issue
This should be called Odd Aggregation Issue. I am almost certain it's not related to inheritance, which is nested further up in the MemberProfile and Gender objects (which also work file). --~--~-~--~~~---~--~~ 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: unions and order_by
The Controlling Ordering section of the docs mentions that using order_by on mappers is the standard way for setting a default ordering against a single mapped entity. This seems like a good feature. Is there another way? Will this be deprecated in the future? What's also really weird is that if I put my __mapper_args__ at the beginning of the definition of Anything then I get the following errors for q1.union(q2), query.union(q1, q2) and query.union_all(q1, q2): File /usr/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/ sqlalchemy/sql/visitors.py, line 247, in clone cloned[element] = element._clone() AttributeError: 'str' object has no attribute '_clone' If I put __mapper_args__ at the end it passes through. Also if I use the original: u = union(q1.statement, q2.statement) print list(session.query(Anything).from_statement(u)) ... then it doesn't matter where the __mapper_args__ are. And for posterity, this is where the order_by(None)'s should be: q1 = session.query(Anything).join('somethings').order_by(None) q2 = session.query(Anything).join('somethings').order_by(None) q = session.query(Anything).order_by(None).union(q1, q2) --~--~-~--~~~---~--~~ 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] INTERVAL 1 DAY in ORM
Hi ! I get some query: DELETE FROM passwordrequest WHERE requested_at = (NOW() - INTERVAL 1 DAY) which delete all requests older then one day. How can I get this effect in SQLAlchemy ? Greetings from Poland Jan Koprowski --~--~-~--~~~---~--~~ 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] oracle flashback query
Hi, I'm wondering if there's a way I can add a string to the tablename in a select query without affecting reflection, aliases, etc. Eg. given: t = Table( 'c1', column( 'c1', Integer, primary_key=True) I'd like to be able to generate: SELECT scntest.c1 AS scntest_c1 FROM scntest AS OF SCN 123 maybe using something like session.query( t.from_append( AS OF SCN 123 )).all() --~--~-~--~~~---~--~~ 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: INTERVAL 1 DAY in ORM
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 26.01.2009 6:23 Uhr, Jan Koprowski wrote: Hi ! I get some query: DELETE FROM passwordrequest WHERE requested_at = (NOW() - INTERVAL 1 DAY) which delete all requests older then one day. How can I get this effect in SQLAlchemy ? untested: session = ... PR = mapper for passwordrequest for pr in session.query(PR).filter(PR.requested_at=(datetime.now()-datetime.timedelta(1)): session.delete(pr) - -aj -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkl9UuQACgkQCJIWIbr9KYwtwQCgwciGZ91zVag/s9JRrQMdOXk2 nl4AoIz5ixXW9h3L4U7D2z6rMpmtwv9s =oFIk -END PGP SIGNATURE- --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~--- begin:vcard fn:Andreas Jung n:Jung;Andreas org:ZOPYX Ltd. Co. KG adr;quoted-printable:;;Charlottenstr. 37/1;T=C3=BCbingen;;72070;Germany email;internet:i...@zopyx.com title:CEO tel;work:+49-7071-793376 tel;fax:+49-7071-7936840 tel;home:+49-7071-793257 x-mozilla-html:FALSE url:www.zopyx.com version:2.1 end:vcard