[sqlalchemy] activemapper runs into problems with particular import ordering
I've just overcome a problem in a test app that's using activemapper. At the top, the imports were done like this: from sqlalchemy.ext.activemapper import * from sqlalchemy import * Running my script gave this error: $ python test.py Traceback (most recent call last): File test.py, line 4, in ? class release(ActiveMapper): File test.py, line 5, in release class mapping: File test.py, line 6, in mapping id = column(Integer, primary_key=True) TypeError: column() got an unexpected keyword argument 'primary_key' $ When I changed around the imports to this: from sqlalchemy import * from sqlalchemy.ext.activemapper import * .. it worked fine. I'm not sure how this should be fixed (if at all), but thought the activemapper users might be interested in it. It's likely I should be doing my imports differently. I had strict 'import blah' statements originally but shied away from it because it wasn't clear which bits were coming from where. - C --~--~-~--~~~---~--~~ 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 a graph data structure
On 11/1/06, Michael Bayer [EMAIL PROTECTED] wrote: im beginning to regret having viewonly and non_primary as options,since i cant think of anything they do that cant be better accomplishedjust by using Query.select(), or manual queries in conjunction with query.instances().I think im going to try to express this in thedocumentation...Nice, thanks. The version you pasted is exactly the elegant way I wanted to find* I hope this was a helpful excercise for others besides myself. :)Arnar* on a philosophical note: are programs, like mathematical findings, found or made? One could look at it in such a way that the method which a program uses to solve a problem already exists - the job of a programmer is only to discover it and express it in some form - not to create it :) --~--~-~--~~~---~--~~ 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] serialization problem ?
Hello ! I'm running SQLAlchemy 0.2.8 with Python 2.4. I have the following : roles_table = Table('roles', meta, Column('id', Integer, primary_key=True, nullable=False), Column('name', String(100), nullable=False)) users_table = Table('users', meta, Column('id', Integer, primary_key=True, nullable=False), Column('added', DateTime, nullable=False, default=func.localtimestamp), Column('modified', DateTime, nullable=False, default=func.localtimestamp, onupdate=func.localtimestamp), Column('first_name', String(200), nullable=False), Column('last_name', String(200), nullable=False), Column('login', String(200), nullable=False), Column('password', String(40), nullable=False), Column('email', String(200), nullable=False)) user_role_table = Table('user_role', meta, Column('role_id', Integer, ForeignKey('roles.id'), primary_key=True, nullable=False), Column('user_id', Integer, ForeignKey('users.id'), primary_key=True, nullable=False)) mapper(role.Role, roles_table) mapper(authentication.User, users_table, properties = { 'roles' : relation(role.Role, secondary=user_role_table, cascade='all, delete-orphan'), '_id' : users_table.c.id, '_added': users_table.c.added, '_modified' : users_table.c.modified, '_first_name' : users_table.c.first_name, '_last_name': users_table.c.last_name, '_login': users_table.c.login, '_password' : users_table.c.password, '_email': users_table.c.email }) The situation is the following: I'm using the MVC pattern with mod_python (with a home made handler). BaseController is the root class for all the controllers. Every action is rendered through the .render() method in the BaseController. I have a controller called UserController with a method .login() - if the login is successfull I store an User object (which is the authenticated user) in the user (mod_python) session. The problem I have is that when I reload the session it fail with the following error : PythonHandler public/dispatch: Traceback (most recent call last): PythonHandler public/dispatch: File /usr/lib/python2.4/site-packages/mod_python/apache.py, line 299, in HandlerDispatch\nresult = object(req) PythonHandler public/dispatch: File /home/jcigar/public_html/bbpf_website/trunk/public/dispatch.py, line 34, in handler\nctrl_inst = ctrl_class(req) PythonHandler public/dispatch: File /home/jcigar/public_html/bbpf_website/trunk/application/controllers/site.py, line 16, in __init__\nsuper(SiteController, self).__init__(req) PythonHandler public/dispatch: File /home/jcigar/public_html/bbpf_website/trunk/application/controllers/base.py, line 18, in __init__\nself.session_user = Session.DbmSession(self.req, dbm=configuration.main.SESSION_LOCATION) PythonHandler public/dispatch: File /usr/lib/python2.4/site-packages/mod_python/Session.py, line 337, in __init__\ntimeout=timeout, lock=lock) PythonHandler public/dispatch: File /usr/lib/python2.4/site-packages/mod_python/Session.py, line 166, in __init__\nif self.load(): PythonHandler public/dispatch: File /usr/lib/python2.4/site-packages/mod_python/Session.py, line 225, in load\ndict = self.do_load() PythonHandler public/dispatch: File /usr/lib/python2.4/site-packages/mod_python/Session.py, line 361, in do_load\nreturn cPickle.loads(dbm[self._sid]) PythonHandler public/dispatch: File /usr/lib/python2.4/site-packages/sqlalchemy/attributes.py, line 337, in __setstate__\nself.attr = getattr(d['obj'].__class__, self.key) PythonHandler public/dispatch: AttributeError: type object 'User' has no attribute 'roles' Is it possible that the Relation() roles of the User mapper is not serializable .. ? Here is my code : import os.path from sqlalchemy import create_session from genshi.template import TemplateLoader from genshi.core import Markup from mod_python import util, Session, apache from application.models.authentication import User, AnonymousUser import configuration class BaseController(object): def __init__(self, req): self.req= req self.session_db = create_session() # THIS IS THE LINE WHICH FAIL self.session_user = Session.DbmSession(self.req, dbm=configuration.main.SESSION_LOCATION) self.user = self.session_user.get('user', AnonymousUser()) self.errors = {} self.params = {} self.context= {} try: # detached - persistent// self.session_db.update(self.user) except: pass def render(self, template): self.context.update(Markup=Markup, user=self.user, SITE_ROOT=configuration.main.SITE_ROOT, MEDIA_ROOT=configuration.main.MEDIA_ROOT, request=self.req, errors=self.errors) loader = TemplateLoader([configuration.main.TEMPLATES_LOCATION]) template
[sqlalchemy] Re: New Plugin - AssociationProxy
This is really awesome. It would be even awesomer if append()/create() could optionally take keyword arguments that would be passed to the creator, so that the creator would be able to populate other association fields if it knows how. I'm thinking of something like: post.tags.append('awesomer', user=me) If that seems interesting to others, I can probably whip up a patch this weekend (though it's been a while since I got my hands dirty on the insides of sqlalchemy). JP --~--~-~--~~~---~--~~ 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: Multilpe Table inheritance and relations
Since I'm really stupid, I have another problem which seems to be much to complex for my small brain. I'm performing some more tests with SA and I tried to add a self reference to a table which also is involved in inheritance. This is my example : employees = Table('employees', metadata, Column('person_id', Integer, primary_key=True), Column('father_id', Integer, ForeignKey('employees.person_id')), Column('name', String(50)), Column('type', String(30))) engineers = Table('engineers', metadata, Column('person_id', Integer, ForeignKey('employees.person_id'), primary_key=True), Column('engineer_info', String(50)), ) person_join = polymorphic_union( { 'engineer':employees.join(engineers), 'person':employees.select(employees.c.type=='person'), }, None, 'pjoin') person_mapper = mapper( Employee, employees, select_table=person_join, polymorphic_on=person_join.c.type, polymorphic_identity='person', properties={'father': relation( Employee, primaryjoin=employees.c.another_id==employees.c.person_id, backref='children', uselist = False )} ) mapper(Engineer, engineers, inherits=person_mapper, polymorphic_identity='engineer') And then trying to save an object I get a : FlushError: Circular dependency detected Any idea ? Regards, Laurent --~--~-~--~~~---~--~~ 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: Multilpe Table inheritance and relations
Ooops, My primaryjoin=employees.c.another_id==employees.c.person_id should be primaryjoin=employees.c.father_id==employees.c.person_id. This is just a typo. I really have this FlushError: Circular dependency detected --~--~-~--~~~---~--~~ 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: Multilpe Table inheritance and relations
this error implies that two instances are dependent on each other, and cannot both be INSERTed whole. the postupdate flag is used to remedy this situation, which will issue a second UPDATE statement to associate the two rows together after they have been INSERTed. if thats not working, attach a fully functioning test script to your email, since using the above mappings i can save instances no problem (its the specific structure you want to save that i'd need to see). --~--~-~--~~~---~--~~ 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: serialization problem ?
the fact that a User class has an attribute roles is due to the mapper placed on the User class, which initializes it with the roles attribute. if youre using mod_python, its likely using a multi-process model which can deserialize a User object into a new process where the owning module has not yet been imported, nor have any mappers been set up; pickle then imports the module on its own during the load() and is likely not initializing the mappers. you need to insure that either your mod_python process always imports your User class as well as initilalizes the mappers on it (and calls compile() on at least one of the mappers) before doing any pickle operations, or that simply importing User's module will also set up the mappers. as a test, just write a script that serializes the mapped User to a file. then go into a python shell and try to open the file and pickle.load() it, without any imports. --~--~-~--~~~---~--~~ 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] generating only SQL statements?
All, I'm writing code that is using mappers for a database. I'm currently going in and creating objects based off of other objects that will then need to be deleted. (Meaning I'm adding records to tables based on other records that will be removed entirely). I'm wondering if there is already a method to take an object that represents a row and generate delete, insert or update statements from it. OR if there is a way to run through my script and instead of session.flush() actually making changes to the database, having it generate the SQL that needs to be done, and not touch the database. That way a human can go back through and verify the changes before committing them to the database. Brian --~--~-~--~~~---~--~~ 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] Error when changing two row values
I'm new to both python and SQLAlchemy, but am improving. I'm building a new application that will use a sqlite database. A unit test is failing with this exception: sqlalchemy.exceptions.ConcurrentModificationError: Updated rowcount 0 does not match number of objects updated 1 Below is a small version of what I'm trying to do. Is this a SQLAlchemy bug or do I need to be doing something differently? The error happens on the final flush() call. import sqlalchemy as sqla db_con = sqla.create_engine('sqlite:///:memory:') metadata = sqla.BoundMetaData(db_con) signal_types = sqla.Table('signal_types', metadata, sqla.Column('signal_type_id', sqla.Integer, primary_key = True), sqla.Column('signal_type_name', sqla.String(25), nullable = False, unique = True), sqla.UniqueConstraint('signal_type_name', name = 'signal_types_idx1') ) signal_enumerations = sqla.Table('signal_enumerations', metadata, sqla.Column('signal_type_id', sqla.Integer, sqla.ForeignKey('signal_types.signal_type_id'), nullable = False), sqla.Column('signal_enumeration_name', sqla.String(50), nullable = False), sqla.Column('signal_enumeration_value', sqla.Integer, nullable = False), sqla.PrimaryKeyConstraint('signal_type_id', 'signal_enumeration_name') ) metadata.create_all() session = sqla.create_session(db_con) class Signal_type(object): Model class for the Signal_types table def __init__(self, name): self.signal_type_name = name class Signal_enumeration(object): Model class for the Signal_enumeration table def __init__(self, name, value): self.signal_enumeration_name = name self.signal_enumeration_value = value def __repr__(self): return %s: %d % (self.signal_enumeration_name, self.signal_enumeration_value) # Table signal_types # Primary Key: signal_type_id signal_types_mapper = sqla.mapper(Signal_type, signal_types) # Table signal_enumerations # Primary Key: signal_enumeration_name # Foreign Key: signal_types(signal_type_id) sqla.mapper(Signal_enumeration, signal_enumerations) signal_types_mapper.add_property('enumerations', sqla.relation(Signal_enumeration, order_by = signal_enumerations.c.signal_enumeration_value, cascade = all, delete-orphan)) ### # Add database data new_type = Signal_type('enum_type') new_type.enumerations.append(Signal_enumeration('one', 1)) new_type.enumerations.append(Signal_enumeration('two', 2)) new_type.enumerations.append(Signal_enumeration('three', 3)) new_type.enumerations.append(Signal_enumeration('four', 4)) session.save(new_type) session.flush() new_type.enumerations[2].signal_enumeration_name = 'thirty' #session.flush() new_type.enumerations[2].signal_enumeration_value = 30 session.flush() --~--~-~--~~~---~--~~ 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: convert_unicode=True results in double encoding
On 11/3/06, Shannon -jj Behrens [EMAIL PROTECTED] wrote: I'm using convert_unicode=True. Everything is fine as long as I'm the one reading and writing the data. However, if I look at what's actually being stored in the database, it's like the data has been encoded twiced. If I switch to use_unicode=True, which I believe is MySQL specific, things work just fine and what's being stored in the database looks correct. I started looking through the SQLAlchemy code, and I came across this: def convert_bind_param(self, value, dialect): if not dialect.convert_unicode or value is None or not isinstance(value, unicode): return value else: return value.encode(dialect.encoding) def convert_result_value(self, value, dialect): if not dialect.convert_unicode or value is None or isinstance(value, unicode): return value else: return value.decode(dialect.encoding) The logic looks backwards. It says, If it's not a unicode object, return it. Otherwise, encode it. Later, If it is a unicode object, return it. Otherwise decode it. Am I correct that this is backwards? If so, this is going to be *painful* to update all the databases out there! Ok, MySQLdb doesn't have a mailing list, so I can't ask there. Here are some things I've learned: Changing from convert_unicode=True to use_unicode=True doesn't do what you'd expect. SQLAlchemy is passing keyword arguments all over the place, and use_unicode actually gets ignored. minor rantI personally think that you should be strict *somewhere* when you're passing around keyword arguments. I've been bitten in this way too many times. Unknown keyword arguments should result in exceptions./minor rant Anyway, I'm still a bit worried about that code above like I said. However, here's what's even scarier. If I use the following code: import MySQLdb for use_unicode in (True, False): connection = MySQLdb.connect(host=localhost, user=user, passwd='dataase', db=users, use_unicode=use_unicode) cursor = connection.cursor() cursor.execute(select firstName from users where username='test') row = cursor.fetchone() print use_unicode:%s %r % (use_unicode, row) I get use_unicode:True (u'test \xc3\xa7',) use_unicode:False ('test \xc3\xa7',) Notice the result is the same, but one has a unicode object and the other doesn't. Notice that it's \xc3\xa7 each time? It shouldn't be. Consider: s = 'test \xc3\xa7' s.decode('utf-8') u'test \xe7' *It's creating a unicode object without actually doing any decoding!* This is somewhere low level. Like I said, this is lower level than SQLAlchemy, but I don't have anywhere else to turn. SQLAlchemy: 0.2.8 MySQLdb: 1.36.2.4 mysql client and server: 5.0.22 Ubuntu: 6.0.6 Help! -jj -- http://jjinux.blogspot.com/ --~--~-~--~~~---~--~~ 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: single table inheritance questions
Michael Bayer wrote: On Nov 2, 2006, at 11:47 PM, Randall Smith wrote: Is there a way to inherit more than one level for single table inheritance? Take this relationship for example: Animal - Dog - German Shepard Say there are 10 animals; 5 are dogs and 2 are German Shepard. session.query(Animal).select() # Should yield 10 results. session.query(Dog).select() # Should yield 5 results. session.query(GermanShepard).select() # Should yield 2 results. not unless you apply the attached patch, and/or update to rev 2084. Works nicely. Thanks! To me, it seems that getting inheritance right is important to fully utilizing the O in ORM and I like you implementation so far. Randall --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---