Re: [sqlalchemy] SQLAlchemy.func.max()
Michael Bayer wrote: jo wrote: I was using heavily the column_prefix and my code is full of it, as in: mapper(Anagrafica, tbl['anagrafica'], column_prefix = 'anagrafica_', extension=History(), properties = { 'comune' : relation( Comune, primaryjoin = tbl['anagrafica'].c.id_comune == tbl['comune'].c.id ), 'nazione': relation( Nazione, primaryjoin = tbl['anagrafica'].c.cod_cittadinanza == tbl['nazione'].c.codic... 'comune_nascita' : relation( Comune, primaryjoin = tbl['anagrafica'].c.id_comune_nascita == tbl['comune'].c.id), 'nazione_nascita': relation( Nazione, primaryjoin = tbl['anagrafica'].c.cod_nazione_nascita == tbl['nazione'].c.co... 'professione': relation( Professione ), 'titolo_studio' : relation( TitoloStudio ), } ) column_prefix remains available. That above code is compatible with 0.6. I tried upgrade from 0.3 to 0.4 and if I remember well, there wasn't column_prefix in 0.4. Are you suggest me to migrate directly from 0.3 to 0.6 instead of a gradual migration? j -- 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] Re: relation to arbitrary class (hibernate any)
On Apr 7, 9:29 pm, Michael Bayer mike...@zzzcomputing.com wrote: Tvrtko wrote: I have a schema that cannot be changed. In it, there is a table with two columns: element_type CHAR(1) element_id INTEGER There is no foreign key on element_id because it can point to different, unrelated tables (element_type) says where element_id points to. How can I model this in sqlalchemy? this is a rails-style polymorphic association (note that Hibernate calls it this as well: The any mapping element defines a polymorphic association to classes from multiple tables.). See the example examples/poly_assoc/poly_assoc.py in the distro as well as the discussion athttp://techspot.zzzeek.org/?p=13. Thanks. It s good to know that I can just pretend that there are foreign keys between the tables. This actually helps in more cases than just this one! I know how to load and store element using property: def _get_element(self): if self.need_element_reload(): clazz = self.get_class_from_type(self._element_type) self._element = object_session(self).query(clazz).get(self.element_id) return self._element def _set_element(self, element): self._element_type = self.get_type_from_instance(element) self._element_id = element.id if element else None self._element = element element = property(_get_element, _set_element) But this is not good enough. For example, if I create new element which is not yet flushed, there is no id. Normally, sqlalchemy knows about relationships and finishes the foreign key column assignment once the pointed-to id is known. How can I model this type of relationship so that sqlalchemy will do the right thing (assign element_id in a flush process, or more generally behave like this is a normal relationship). I'm not sure if I explained it correctly, so I'll just mention that hibernate has any element that can do just that. Thanks, Tvrtko -- 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] SQLCLCHEMY -WINDOWS
Hi, I am a student and I am working on a project which uses python, sqlalchemy and psqlite on windows. The problem is python is running fine but I am not able to use sqlalchemy. I have tries searching but no links give proper working steps to be followed in WINDOWS. Could someone please tell me the simple step by step procedure to follow while installing and using sqlalchemy? Thanks -- 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] SQLCLCHEMY -WINDOWS
Hi, On 08/04/2010 08:18, prateeksha wrote: Hi, I am a student and I am working on a project which uses python, sqlalchemy and psqlite on windows. The problem is python is running fine but I am not able to use sqlalchemy. I have tries searching but no links give proper working steps to be followed in WINDOWS. Could someone please tell me the simple step by step procedure to follow while installing and using sqlalchemy? I use a little script for my .egg installs. import pkg_resources pkg_resources.require(setuptools) from setuptools.command import easy_install sitePKG = rc:\Python25\Lib\site-packages ##egg = amara - does not work for me yet egg = sqlalchemy ##egg = Sphinx # MULTI or SINGLE?!!! # # m = multi # Z = always unzip # a = always copy # x = exclude scripts # d = install directory ### to uninstall ##easy_install.main(['-m', sitePKG, egg]) ### to install - Multiversion ##easy_install.main(['-Zmaxd', sitePKG, egg]) ### to install - Multiversion WITH SCRIPTS easy_install.main(['-Zmad', sitePKG, egg]) Hope this helps Werner -- 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] SQLCLCHEMY -WINDOWS
On 08/04/2010 15:02, werner wrote: Hi, On 08/04/2010 08:18, prateeksha wrote: Hi, I am a student and I am working on a project which uses python, sqlalchemy and psqlite on windows. The problem is python is running fine but I am not able to use sqlalchemy. I have tries searching but no links give proper working steps to be followed in WINDOWS. Could someone please tell me the simple step by step procedure to follow while installing and using sqlalchemy? I use a little script for my .egg installs. import pkg_resources pkg_resources.require(setuptools) from setuptools.command import easy_install sitePKG = rc:\Python25\Lib\site-packages ##egg = amara - does not work for me yet egg = sqlalchemy ##egg = Sphinx # MULTI or SINGLE?!!! # # m = multi # Z = always unzip # a = always copy # x = exclude scripts # d = install directory ### to uninstall ##easy_install.main(['-m', sitePKG, egg]) ### to install - Multiversion ##easy_install.main(['-Zmaxd', sitePKG, egg]) ### to install - Multiversion WITH SCRIPTS easy_install.main(['-Zmad', sitePKG, egg]) Hope this helps Werner Forgot to mention that if you use Multiversion then you need this to do something like this when using/importing it: import sys if not hasattr(sys, 'frozen'): import pkg_resources pkg_resources.require(sqlalchemy) # get latest version ##pkg_resources.require(sqlalchemy==0.5.8) # get specific version import sqlalchemy as sa Werner -- 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] mixins vs single table inheritence
Hi Again, So here's a test case ready to drop onto the end of test_declarative.py: def test_mapper_args_single_table(self): class TableNameMixin: @classproperty def __tablename__(cls): return cls.__name__.lower() class CommonColumns: id = Column(Integer, primary_key=True) class BaseType(Base, TableNameMixin, CommonColumns): discriminator = Column('python_type', String(50)) __mapper_args__= dict(polymorphic_on=discriminator) value = Column(Integer()) class SpecificType1(BaseType): __mapper_args__ = dict(polymorphic_identity='type1') class SpecificType2(BaseType): __mapper_args__ = dict(polymorphic_identity='type2') In 0.5.8 with the mixin recipe, this gives: File test_mixable.py, line 459, in test_mapper_args_single_table class SpecificType1(BaseType): File mixable.py, line 46, in __init__ DeclarativeMeta.__init__(cls, classname, bases, dict_) File /usr/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/ext/declarative.py, line 561, in __init__ _as_declarative(cls, classname, dict_) File /usr/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/ext/declarative.py, line 494, in _as_declarative *(tuple(cols) + tuple(args)), **table_kw) File /usr/lib/python2.5/site-packages/SQLAlchemy-0.5.8-py2.5.egg/sqlalchemy/schema.py, line 99, in __call__ columns on an existing Table object. % key) sqlalchemy.exc.InvalidRequestError: Table 'basetype' is already defined for this MetaData instance. Specify 'useexisting=True' to redefine options and columns on an existing Table object. On trunk, it gives: == ERROR: test.ext.test_declarative.DeclarativeMixinTest.test_mapper_args_single_table -- Traceback (most recent call last): File /mnt/Users/chris.withers/sqlalchemy_env/lib/python2.6/site-packages/nose-0.11.3-py2.6.egg/nose/case.py, line 186, in runTest self.test(*self.arg) File /mnt/Users/chris.withers/sqlalchemy/test/ext/test_declarative.py, line 2228, in test_mapper_args_single_table class SpecificType1(BaseType): File /mnt/Users/chris.withers/sqlalchemy/lib/sqlalchemy/ext/declarative.py, line 725, in __init__ _as_declarative(cls, classname, cls.__dict__) File /mnt/Users/chris.withers/sqlalchemy/lib/sqlalchemy/ext/declarative.py, line 682, in _as_declarative ignore_nonexistent_tables=True) File /mnt/Users/chris.withers/sqlalchemy/lib/sqlalchemy/sql/util.py, line 205, in join_condition between '%s' and '%s'.%s % (a.description, b.description, hint)) ArgumentError: Can't find any foreign key relationships between 'basetype' and 'specifictype1'. *sigh* Any ideas? (I'll be looking further into this, but thought I'd chuck it at the tlist in case I'm missing anything obvious) cheers, Chris -- 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] SQLAlchemy.func.max()
jose soares wrote: Michael Bayer wrote: jo wrote: I was using heavily the column_prefix and my code is full of it, as in: mapper(Anagrafica, tbl['anagrafica'], column_prefix = 'anagrafica_', extension=History(), properties = { 'comune' : relation( Comune, primaryjoin = tbl['anagrafica'].c.id_comune == tbl['comune'].c.id ), 'nazione': relation( Nazione, primaryjoin = tbl['anagrafica'].c.cod_cittadinanza == tbl['nazione'].c.codic... 'comune_nascita' : relation( Comune, primaryjoin = tbl['anagrafica'].c.id_comune_nascita == tbl['comune'].c.id), 'nazione_nascita': relation( Nazione, primaryjoin = tbl['anagrafica'].c.cod_nazione_nascita == tbl['nazione'].c.co... 'professione': relation( Professione ), 'titolo_studio' : relation( TitoloStudio ), } ) column_prefix remains available. That above code is compatible with 0.6. I tried upgrade from 0.3 to 0.4 and if I remember well, there wasn't column_prefix in 0.4. Are you suggest me to migrate directly from 0.3 to 0.6 instead of a gradual migration? if it were me I'd certainly go straight to 0.6. I'd be curious to know what kinds of code patterns you think would be difficult to migrate (I haven't used 0.3 in years obviously). j -- 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. -- 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] mixins vs single table inheritence
Chris Withers wrote: Hi Again, So here's a test case ready to drop onto the end of test_declarative.py: def test_mapper_args_single_table(self): class TableNameMixin: @classproperty def __tablename__(cls): return cls.__name__.lower() class CommonColumns: id = Column(Integer, primary_key=True) class BaseType(Base, TableNameMixin, CommonColumns): discriminator = Column('python_type', String(50)) __mapper_args__= dict(polymorphic_on=discriminator) value = Column(Integer()) class SpecificType1(BaseType): __mapper_args__ = dict(polymorphic_identity='type1') class SpecificType2(BaseType): __mapper_args__ = dict(polymorphic_identity='type2') between '%s' and '%s'.%s % (a.description, b.description, hint)) ArgumentError: Can't find any foreign key relationships between 'basetype' and 'specifictype1'. *sigh* Any ideas? (I'll be looking further into this, but thought I'd chuck it at the tlist in case I'm missing anything obvious) are you wondering*why* this happens ? its because your mixin is giving all classes a __tablename__. If you want to force SpecificType1/2 to be single-inherited from Base, they'd need their own __tablename__ set to None. Which you can achieve declaratively in each class or via another mixin. cheers, Chris -- 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. -- 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] mixins vs single table inheritence
Michael Bayer wrote: are you wondering*why* this happens ? its because your mixin is giving all classes a __tablename__. Well, that's only a symptom of the problem, here's another example: def test_mapper_args_single_table(self): class CommonColumns: id = Column(Integer, primary_key=True) class BaseType(Base, CommonColumns): __tablename__ = 'base' discriminator = Column('python_type', String(50)) __mapper_args__= dict(polymorphic_on=discriminator) value = Column(Integer()) class SpecificType1(BaseType): __mapper_args__ = dict(polymorphic_identity='type1') class SpecificType2(BaseType): __mapper_args__ = dict(polymorphic_identity='type2') == ERROR: test.ext.test_declarative.DeclarativeMixinTest.test_mapper_args_single_table -- Traceback (most recent call last): File /mnt/Users/chris.withers/sqlalchemy_env/lib/python2.6/site-packages/nose-0.11.3-py2.6.egg/nose/case.py, line 186, in runTest self.test(*self.arg) File /mnt/Users/chris.withers/sqlalchemy/test/ext/test_declarative.py, line 2224, in test_mapper_args_single_table class SpecificType1(BaseType): File /mnt/Users/chris.withers/sqlalchemy/lib/sqlalchemy/ext/declarative.py, line 725, in __init__ _as_declarative(cls, classname, cls.__dict__) File /mnt/Users/chris.withers/sqlalchemy/lib/sqlalchemy/ext/declarative.py, line 696, in _as_declarative Can't place primary key columns on an inherited class with no table. ArgumentError: Can't place primary key columns on an inherited class with no table. I'm thinking that declarative should ignore columns, __table_args__ and __tablename__ from mixins where a base of the class being dealt with is already mapped (implying some kind of table inheritence, right?). I can see situations where you'd want mixins to work as they currently do (which causes the above test failure) with concrete and joined table inheritence, but in that case, you can just use the mixin on both the parent and the child classes, right? cheers, Chris -- 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] binding values for the in() statement
Hello, I am trying to bind a list for an in() operator in a pure SQL query : import sqlalchemy engine = create_engine('mysql://localhost/database') connection = engine.connect() connection.execute(engine.text(select * from user where email in (:emails)), emails=['ta...@ziade.org']) This will fail because the dialect will not bind a list for the :emails params. Is there a way to do this ? I looked at the expression compiler but didn't find anything relevant. Or do I have to build the query manually with a ','.join() in this case ? Regards, Tarek -- Tarek Ziadé | http://ziade.org -- 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] binding values for the in() statement
Tarek Ziadé wrote: Hello, I am trying to bind a list for an in() operator in a pure SQL query : import sqlalchemy engine = create_engine('mysql://localhost/database') connection = engine.connect() connection.execute(engine.text(select * from user where email in (:emails)), emails=['ta...@ziade.org']) This will fail because the dialect will not bind a list for the :emails params. Is there a way to do this ? I looked at the expression compiler but didn't find anything relevant. Or do I have to build the query manually with a ','.join() in this case ? you do. bind parameters don't expand into arrays with relational databases. Options to get around this include, putting a subquery into your in(), or of course SQLA's in_() operator generates the bind parameters for you. Regards, Tarek -- Tarek Ziadé | http://ziade.org -- 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. -- 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] NameError: global name 'exc' is not defined (0.6beta3)
Thanks, I took a clone from hg, and all of my unit tests are passing now, and 0.6 is working great. I ran into one instance (I don't think this is a bug) where session.expire_all() didn't clear out an object after a call to Query.delete(synchronize_session=False), but I found that using session.expunge_all() instead worked around the issue. On 04/07/2010 01:08 PM, Michael Bayer wrote: these issues are all repaired as of r9b02710ca3b3. -- David Gardner Pipeline Tools Programmer Jim Henson Creature Shop dgard...@creatureshop.com -- 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] label()
I'm doing my first steps with sqlalchemy - a really great package? Here is my question: I know already how to label columns or functions that I'm querying for, like this: session.query(count().label(foo), MyClass.id.label(my_id)) How can I label the result when I search for a class? This doesn't work: session.query(MyClass.label(spam)) -- Thanks, Thomas -- 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] label()
Thomas Heller wrote: I'm doing my first steps with sqlalchemy - a really great package? Here is my question: I know already how to label columns or functions that I'm querying for, like this: session.query(count().label(foo), MyClass.id.label(my_id)) How can I label the result when I search for a class? This doesn't work: session.query(MyClass.label(spam)) use my_alias = aliased(MyClass, name=spam) then use my_alias instead of MyClass throughout your query. -- Thanks, Thomas -- 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. -- 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] mapper for ms sql server
Hello. Recently I have been working on this environment: linux + pyodbc + sqlalchemy 5.8 + ms sql server. In the program, I try to build a mapper from a db table to my python class, then I instatiate the class, and try to use session.add() to insert record to database. However, I need to have alter any schema permission in order to make it through, If I do not have this permission, I will get File C:\Python25\lib\site-packages\sqlalchemy\databases\mssql.py, line 954, in pre_exec self.dialect.identifier_preparer.format_table(self.compiled.statement.table)) ProgrammingError: ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot find the object Sources because it does not exist or you do not have permissions. (1088) (SQLExecDirectW)') exception. wondering is If there is any other way to work around this issue, assuming no alter any schema permission given. Thanks! ---Tony -- 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] How can i build a connection wtih Persistent
Hi,all I have a cronjob to run,and it may be executed over half an hour... but at 10 min I found i lost connection from mysql,And How can i build a connection wtih Persistent? -- 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.