Re: [sqlalchemy] Declarative syntax with column_property (v0.6.3)
Hi Michael, I'm guilty of two over-simplifications in the code used in my example... Firstly, the substring property/column (with which I had a problem) applies to a subclass (using joined table inheritance) and the variable to which it applies is specified in its parent class. Hence, I started by assuming that I'd need something along the lines of: class Parent(Base): my_string class Child(Parent): @classproperty/@property/ some other decorator def my_substr(cls): return column_property ( cls.my_string ) However, trying this (ie the classproperty/column_property within Child), returned sqlalchemy.orm.properties.ColumnProperty object ... (the error I referred to in my original question) I'm confused about why I'm not able to get this (or some variation of it) to work. Is this indeed possible? Looking at the documentation (as a consequence of the error), my I was drawn to the syntax used in the section on 'mixins' (ie, the column_property defined externally). My second over-simplification regards the the function itself. The function I'm trying to use [which I'm aware doesn't translate easily to sqlite], is: func.substr(cls.my_str, func.instr(cls.my_str, )+1, 2) Now, thanks to your response, I now actually have the following *working* (using mysql): class MySubStr(object): @classproperty def my_substr(cls): return column_property( func.substr(cls.my_string, func.instr(cls.my_string, )+1, 2) ) However, replacing: func.substr(cls.my_string, func.instr(cls.my_string, )+1, 2) with (in order to attach a label to the column): select([func.substr(cls.my_string, func.instr(cls.my_string, )+1, 2)]).label('exch_code') Gives me -- AttributeError: 'NoneType' object has no attribute '_keymap' (full traceback below). Apologies for my earlier over-simplification (and my late response!). And I hope that what I've responded with is clear!! Regards, Rob - /home/robertsudwarts/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.6.3-py2.6.egg/sqlalchemy/orm/query.pyc in one(self) 1520 1521 - 1522 ret = list(self) 1523 1524 l = len(ret) /home/robertsudwarts/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.6.3-py2.6.egg/sqlalchemy/orm/query.pyc in instances(self, cursor, _Query__context) 1667 break 1668 else: - 1669 fetch = cursor.fetchall() 1670 1671 if custom_rows: /home/robertsudwarts/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.6.3-py2.6.egg/sqlalchemy/engine/base.pyc in fetchall(self) 2381 2382 try: - 2383 l = self.process_rows(self._fetchall_impl()) 2384 self.close() 2385 return l /home/robertsudwarts/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.6.3-py2.6.egg/sqlalchemy/engine/base.pyc in process_rows(self, rows) 2364 process_row = self._process_row 2365 metadata = self._metadata - 2366 keymap = metadata._keymap 2367 processors = metadata._processors 2368 if self._echo: AttributeError: 'NoneType' object has no attribute '_keymap' On 2 August 2010 19:42, Michael Bayer mike...@zzzcomputing.com wrote: On Aug 2, 2010, at 1:40 PM, Michael Bayer wrote: On Aug 2, 2010, at 12:19 PM, Robert Sudwarts wrote: Hi, I'm having trouble understanding the correct syntax to be used with Declarative and a column property. The select statement I'm using is: select([func.substr(my_table.c.my_string, 2, 3)]).label(my_substr'), deferred=True And (as per the docs using the expanded syntax, this works as expected. With the Declarative syntax, I've tried: from sqlalchemy.util import classproperty from sqlalchemy.orm..., column_property class MySubstr(object): @classproperty def my_substr(cls): return column_property( select([func.substr(cls.my_str, 2, 3)]).label('my_substr') ) class MyTable(Base, MySubstr): . and then expect to be able to call a record in MyTable and return its my_substr, however, all I'm getting is a representation of the object ... eg sqlalchemy.orm.properties.ColumnProperty object at 0xa4951cc and no apparent way of seeing its value. oh and also, you don't need the select() here either: class MyTable(Base): __tablename__ = 'foo' id = Column(Integer, primary_key=True) my_str = Column('my_str', String) my_substr = column_property(func.substr(cls.my_str, 2, 3)) I see nothing wrong with the example, the only potential glitch is that my_str needs to have a name assigned up front. You didn't illustrate that part here, so FYI it would be extremely
Re: [sqlalchemy] Declarative syntax with column_property (v0.6.3)
Please see my comments below. Regards, Rob On 3 August 2010 14:05, Michael Bayer mike...@zzzcomputing.com wrote: On Aug 3, 2010, at 7:47 AM, Robert Sudwarts wrote: Hi Michael, I'm guilty of two over-simplifications in the code used in my example... Firstly, the substring property/column (with which I had a problem) applies to a subclass (using joined table inheritance) and the variable to which it applies is specified in its parent class. Hence, I started by assuming that I'd need something along the lines of: class Parent(Base): my_string class Child(Parent): @classproperty/@property/ some other decorator def my_substr(cls): return column_property ( cls.my_string ) However, trying this (ie the classproperty/column_property within Child), returned sqlalchemy.orm.properties.ColumnProperty object ... (the error I referred to in my original question) I'm confused about why I'm not able to get this (or some variation of it) to work. Is this indeed possible? Just yesterday, in response to this, I added a line of code such that if you specifically use @classproperty on the declarative class (i.e. not a mixin), declarative will call it to figure out what it returns and configure it normally- previously, if the attribute were not directly a MapperProperty, it would be ignored.However, I couldn't think of any use case that this is actually useful for. I thought that, it might be a way to establish a column-oriented property late, so that you could get around imports not being available. At the moment, it's not useful for that, because declarative calls the attribute as soon as the class is created, and whatever imports that werent available to the class declaration are still not present. Ah! Right!! I hadn't understood that ... I assumed (wrongly) that I'd have to 'instantitate' 'cls' somehow using a decorator+function: def blah(cls): ... I didn't realise that I could simply use Parent.my_str So what are you gaining above, if it were working in 0.6.3, by using @classproperty instaed of my_substr = column_property() ? Anywhere you reference cls in your function, you'd just reference whatever is local, or part of Parent. Looking at the documentation (as a consequence of the error), my I was drawn to the syntax used in the section on 'mixins' (ie, the column_property defined externally). My second over-simplification regards the the function itself. The function I'm trying to use [which I'm aware doesn't translate easily to sqlite], is: func.substr(cls.my_str, func.instr(cls.my_str, )+1, 2) Now, thanks to your response, I now actually have the following *working* (using mysql): class MySubStr(object): @classproperty def my_substr(cls): return column_property( func.substr(cls.my_string, func.instr(cls.my_string, )+1, 2) ) However, replacing: func.substr(cls.my_string, func.instr(cls.my_string, )+1, 2) with (in order to attach a label to the column): select([func.substr(cls.my_string, func.instr(cls.my_string, )+1, 2)]).label('exch_code') Gives me -- AttributeError: 'NoneType' object has no attribute '_keymap' (full traceback below). That's a really weird error. It suggests that the result is not one that returns any rows, not like its a SELECT with zero rows, rather, it is perceived by SQLite as something like an INSERT that isn't meant to return any rows, and there's no cursor.description. The ResultProxy is then failing ungracefully which is surprising (looking at the current source it appears we need to cover this situation). It would be interesting to try out the raw SQL to see what is produced, since the statement is definitely a SELECT - pysqlite should raise an error if it doesn't like the SQL, or give us a cursor.description. Just to be clear -- this error/traceback was produced with MySQL as the database Anyway, your column_property() doesn't need the select() here, you can just call .label() on the func.substr() result, but also, you don't really need the label() in here either (unless I'm missing something? ) Again ... I hadn't realised that I could apply .label() to the directly to 'func' ... I've changed this and it works. Apologies for my earlier over-simplification (and my late response!). And I hope that what I've responded with is clear!! Regards, Rob - /home/robertsudwarts/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.6.3-py2.6.egg/sqlalchemy/orm/query.pyc in one(self) 1520 1521 - 1522 ret = list(self) 1523 1524 l = len(ret) /home/robertsudwarts/tg2env/lib/python2.6/site-packages/SQLAlchemy-0.6.3-py2.6.egg/sqlalchemy/orm/query.pyc in instances(self, cursor, _Query__context
[sqlalchemy] Declarative syntax with column_property (v0.6.3)
Hi, I'm having trouble understanding the correct syntax to be used with Declarative and a column property. The select statement I'm using is: select([func.substr(my_table.c.my_string, 2, 3)]).label(my_substr'), deferred=True And (as per the docs using the expanded syntax, this works as expected. With the Declarative syntax, I've tried: from sqlalchemy.util import classproperty from sqlalchemy.orm..., column_property class MySubstr(object): @classproperty def my_substr(cls): return column_property( select([func.substr(cls.my_str, 2, 3)]).label('my_substr') ) class MyTable(Base, MySubstr): . and then expect to be able to call a record in MyTable and return its my_substr, however, all I'm getting is a representation of the object ... eg sqlalchemy.orm.properties.ColumnProperty object at 0xa4951cc and no apparent way of seeing its value. Should the @classproperty sit within the MyTable(Base) itself (ie is there a need for the separate object -- and have I been confused by the section on 'mixins' )? I'd be really grateful for any clues! Many thanks, Rob -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@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] Classmethods with Session and Declarative Syntax
Hi, I'm having difficulty understanding how to call/create a session to access a class method of a declaratively defined table/object: essentially my understanding of how session works in this context is at fault... Clearly, the @classmethod requires a **session** (and I realise that: declarative has no integration built in with sessions, and is only intended as an optional syntax for the regular usage of mappers and Table objects) but I'm unsure of how to 'poke' this in. And I'm not sure if model.py require the definition of its own 'local' session, or if/whether this would conflict with the session instantiated in controller.py I'd be grateful for any assistance! What I'm trying to achieve is as follows: *Module #1 -- model.py* from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String(50)) @classmethod def by_name(cls, name): return **Session**.query(cls).filter(cls.name==name).first() def build_db(): '''call this to create the database''' engine = create_engine('sqlite:/// Base.metadata.create_all(engine=engine) *Module #2 - controller.py* from model.py import SomeClass from sqlalchemy import create_engine from sqlalchemy.ormimport sessionmaker, scoped_session engine = create_engine('sqlite:///) Session = scoped_session(sessionmaker(bind=engine, autocommit=False, autoflush=False)) def getUser(username): return SomeClass.by_name(username) -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@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.
Re: [sqlalchemy] Classmethods with Session and Declarative Syntax
Goddit !! Thank you so much!! On 16 June 2010 14:33, Michael Bayer mike...@zzzcomputing.com wrote: On Jun 16, 2010, at 7:58 AM, Robert Sudwarts wrote: Hi, I'm having difficulty understanding how to call/create a session to access a class method of a declaratively defined table/object: essentially my understanding of how session works in this context is at fault... Clearly, the @classmethod requires a **session** (and I realise that: declarative has no integration built in with sessions, and is only intended as an optional syntax for the regular usage of mappers and Table objects) but I'm unsure of how to 'poke' this in. And I'm not sure if model.py require the definition of its own 'local' session, or if/whether this would conflict with the session instantiated in controller.py I'd be grateful for any assistance! What I'm trying to achieve is as follows: *Module #1 -- model.py* from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class SomeClass(Base): __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String(50)) @classmethod def by_name(cls, name): return **Session**.query(cls).filter(cls.name==name).first() def build_db(): '''call this to create the database''' engine = create_engine('sqlite:/// Base.metadata.create_all(engine=engine) *Module #2 - controller.py* from model.py import SomeClass from sqlalchemy import create_engine from sqlalchemy.ormimport sessionmaker, scoped_session engine = create_engine('sqlite:///) Session = scoped_session(sessionmaker(bind=engine, autocommit=False, autoflush=False)) def getUser(username): return SomeClass.by_name(username) your scoped_session would be defined someplace neutral, for example Pylons puts it in a file called model/meta.py, and your classmethod would access it directly as a global variable in meta. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.comsqlalchemy%2bunsubscr...@googlegroups.com . For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@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.