[sqlalchemy] Re: Unstable results using lambda function
the code u've given is quite incomplete. i couldnt get it to work with responses.join(presentations), so i put just responses, and added property presentation=relation(Presentation) and it returns same results. try just executing whole querycount() by hand twice without all the funcs, and see. On Friday 12 December 2008 07:57:13 channing wrote: I have a weird problem: Two identical functions, created using a lambda from the same issuing identical SQL, return two different answers. I'm embarrassed to say this, but it looks stochastic. I'm running 0.4.2p3 with Python 2.5.2 and Ipython 0.8.1. First, the guided tour. I'm using a lambda function to make new, single-argument versions of a multi-argument function: def _parse_fun(fun, fun_args=None): return lambda x: fun.__call__(x,fun_args) Now I have another function that does some filtering and counting: def n_classresps(unit,class_name): return query(ClassResponse).filter_by(unit=unit)\ .filter_by(class_name=class_name).count() And I use _parse_fun to set the value of the class_name argument: yf = _parse_fun(n_classresps,'noise') The problem comes when I make multiple instances: foo = _parse_fun(n_classresps,'noise') Now calling yf and foo returns different results: yf(qunits[0]) returns 0, while foo(qunits[0]) returns 3. I've included a model below that reproduces my problem. The server is a test server that only I'm using, so the database isn't changing. I have this saved to a file, and I run it in IPython 0.8.1 using the run command. After running, if I define foo = _parse_fun(n_classresps,'zf song') And foo(qunits[0]) gives me 3. Then I define bar = _parse_fun(n_classresps,'zf song') Now bar(qunits[0]) gives me 0. See where this is going? The version in my full app reliably gives me the wrong answer, despite issuing the right SQL. I've set engine.echo=True and enabled query logging on the MySQL server, and the SQL is all fine. Running the SQL with engine.execute gives the right answer, as does running the SQL from a command-line tool. Any ideas as to what could be causing this? I am at a complete loss. Thanks, Channing --- Code follows - from sqlalchemy import MetaData, Table, Column, ForeignKey, create_engine, Integer, String, select from sqlalchemy.orm import mapper, relation, ColumnProperty, scoped_session, sessionmaker from sqlalchemy.databases.mysql import MSEnum ''' Set up engine ''' engine = create_engine(*) Session = scoped_session(sessionmaker(autoflush=True, transactional=True, bind=engine)) metadata = MetaData(engine) query = Session.query def _parse_fun(fun,fun_args=None): return lambda x: fun.__call__(x,fun_args) def n_classresps(unit,class_name): return query(ClassResponse)\ .filter_by(unit=unit)\ .filter_by(class_name=class_name)\ .count() class Presentation(object): pass class Response(object): pass class Unit(object): pass class ClassPresentation(Presentation): pass class ClassResponse(Response): pass presentations = Table('presentations', metadata, Column('presentation_id', Integer, primary_key=True, nullable=False), Column('block_id', Integer, nullable=False), Column('type', MSEnum ('single','repeated','class'), nullable=False) ) responses = Table('responses', metadata, Column('response_id', Integer, primary_key=True, nullable=False), Column('unit_id', Integer, ForeignKey('units.unit_id'), nullable=False), Column(u'presentation_id', Integer, ForeignKey('presentations.presentation_id'), nullable=False) ) class_presentations = Table('class_presentations', metadata, Column('presentation_id', Integer, ForeignKey ('presentations.presentation_id'), primary_key=True, nullable=False), Column('class_name', String, nullable=False) ) units = Table('units', metadata, Column('unit_id', Integer, primary_key=True, nullable=False), Column('recsite_id', Integer, nullable=False), Column('type', MSEnum('extracellular','intracellular'), nullable=False) ) mapper(Presentation,presentations, polymorphic_on=presentations.c.type, polymorphic_identity='presentation') rmapper = mapper(Response,responses.join(presentations), polymorphic_on=presentations.c.type,
[sqlalchemy] Re: Variable_TypeByValue(): unhandled data type unicode
it's no good style to expect your input values to be autoconverted somewhere very deep without all the context u may need. Yes, I'm supplying a value directly from the url which is a text value as in: www.myserver/azienda?id=1 I haven't this problem using the postgres adapter because psycopg casting automatically the value, instead cx_Oracle needs an explicit casting. I wonder if there's some parameter to tell cx_Oracle to do it in implict way. --~--~-~--~~~---~--~~ 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: Variable_TypeByValue(): unhandled data type unicode
On Dec 12, 2008, at 5:19 AM, jo wrote: Michael Bayer ha scritto: Look into using the Unicode type for python unicode values. http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html?highlight=unicode#sqlalchemy.Unicode This is the 0.5 documentation I'm using SQLAlchemy version 0.3.10. and cx_Oracle 4.3.1 Seems cx_Oracle isn't able to convert unicode values into integer. Take a look: In [32]: print Azienda.get(1).azienda_id 1 In [33]: print Azienda.id(u'1').azienda_id SQLError: (NotSupportedError) Variable_TypeByValue(): unhandled data type unicode 'SELECT azienda.id AS azienda_id \nFROM azienda \nWHERE azienda.id = :azienda_id ORDER BY azienda.rowid' {'azienda_id': u'1'} 0.3 has the Unicode type as well, the same usage rules apply. However in this case you are supplying a string for an integer value in a query. you have to supply an int, not a string. --~--~-~--~~~---~--~~ 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: Variable_TypeByValue(): unhandled data type unicode
Michael Bayer ha scritto: Look into using the Unicode type for python unicode values. http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html?highlight=unicode#sqlalchemy.Unicode This is the 0.5 documentation I'm using SQLAlchemy version 0.3.10. and cx_Oracle 4.3.1 Seems cx_Oracle isn't able to convert unicode values into integer. Take a look: In [32]: print Azienda.get(1).azienda_id 1 In [33]: print Azienda.id(u'1').azienda_id SQLError: (NotSupportedError) Variable_TypeByValue(): unhandled data type unicode 'SELECT azienda.id AS azienda_id \nFROM azienda \nWHERE azienda.id = :azienda_id ORDER BY azienda.rowid' {'azienda_id': u'1'} j On Dec 11, 2008, at 12:01 PM, jo wrote: Hi all, I'm using SA with cx_Oracle. In some queries it raises the following error: File /usr/lib/python2.4/site-packages/sqlalchemy/engine/base.py, line 581, in _execute_raw self._execute(context) File /usr/lib/python2.4/site-packages/sqlalchemy/engine/base.py, line 599, in _execute raise exceptions.SQLError(context.statement, context.parameters, e) SQLError: (NotSupportedError) Variable_TypeByValue(): unhandled data type unicode 'SELECT comune.data_fine AS comune_data_fine, comune.id AS comune_id, comune.auto_sync_bdn AS comune_auto_sync_bdn, comune.cod_provincia AS comune_cod_provincia, comune.istat AS comune_istat, comune.data_inizio AS comune_data_inizio, comune.cap AS comune_cap, comune.codice_erariale AS comune_codice_erariale, comune.bdn_id AS comune_bdn_id, comune.nome AS comune_nome \nFROM comune \nWHERE comune.id = :comune_id ORDER BY comune.rowid' {'comune_id': u'2895'} Someone could help me? j --~--~-~--~~~---~--~~ 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: Create tables with metadata
SQLALchemy doesn't (directly) contain functions for altering tables. You may be interested in the sqlalchemy-migrate project: http://code.google.com/p/sqlalchemy-migrate/ The first example on this page shows how to add a column: http://code.google.com/p/sqlalchemy-migrate/wiki/MigrateChangeset Hope that helps, Simon -Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com] On Behalf Of jarrod.ches...@gmail.com Sent: 12 December 2008 07:08 To: sqlalchemy Subject: [sqlalchemy] Re: Create tables with metadata Doesn't get created in the database. How do i add columns to tables already defined in the database after i have reflected them into the metadata On Dec 12, 12:59 am, Empty mtr...@gmail.com wrote: Hi, On Thu, Dec 11, 2008 at 8:12 AM, jarrod.ches...@gmail.com jarrod.ches...@gmail.com wrote: Hi I've scoured the documentation and i can't find any info on how to create a column using metadata. from sqlalchemy import engine from sqlalchemy import schema from sqlalchemy import types _config_dbengine = engine.create_engine('sqlite:tmp/db') _config_metadata = schema.MetaData(_config_dbengine, reflect=True) table = _config_metadata.tables['table_name'] table.append_column(schema.Column('id', types.Integer, primary_key=True, autoincrement=True)) This is the steps i'm using but the column doesn't get created. Doesn't get created where? In the database? That's not going to happen. Are you saying it's not included as part of the table definition? Michael --~--~-~--~~~---~--~~ --~--~-~--~~~---~--~~ 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: Variable_TypeByValue(): unhandled data type unicode
On Friday 12 December 2008 12:19:44 jo wrote: Michael Bayer ha scritto: Look into using the Unicode type for python unicode values. http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html ?highlight=unicode#sqlalchemy.Unicode This is the 0.5 documentation I'm using SQLAlchemy version 0.3.10. and cx_Oracle 4.3.1 Seems cx_Oracle isn't able to convert unicode values into integer. who does? are your primary keys strings or what? just do int() Take a look: In [32]: print Azienda.get(1).azienda_id 1 In [33]: print Azienda.id(u'1').azienda_id SQLError: (NotSupportedError) Variable_TypeByValue(): unhandled data type unicode 'SELECT azienda.id AS azienda_id \nFROM azienda \nWHERE azienda.id = :azienda_id ORDER BY azienda.rowid' {'azienda_id': u'1'} j On Dec 11, 2008, at 12:01 PM, jo wrote: Hi all, I'm using SA with cx_Oracle. In some queries it raises the following error: File /usr/lib/python2.4/site-packages/sqlalchemy/engine/base.py, line 581, in _execute_raw self._execute(context) File /usr/lib/python2.4/site-packages/sqlalchemy/engine/base.py, line 599, in _execute raise exceptions.SQLError(context.statement, context.parameters, e) SQLError: (NotSupportedError) Variable_TypeByValue(): unhandled data type unicode 'SELECT comune.data_fine AS comune_data_fine, comune.id AS comune_id, comune.auto_sync_bdn AS comune_auto_sync_bdn, comune.cod_provincia AS comune_cod_provincia, comune.istat AS comune_istat, comune.data_inizio AS comune_data_inizio, comune.cap AS comune_cap, comune.codice_erariale AS comune_codice_erariale, comune.bdn_id AS comune_bdn_id, comune.nome AS comune_nome \nFROM comune \nWHERE comune.id = :comune_id ORDER BY comune.rowid' {'comune_id': u'2895'} Someone could help me? j --~--~-~--~~~---~--~~ 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: Variable_TypeByValue(): unhandled data type unicode
Michael Bayer ha scritto: On Dec 12, 2008, at 5:19 AM, jo wrote: Michael Bayer ha scritto: Look into using the Unicode type for python unicode values. http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/types.html?highlight=unicode#sqlalchemy.Unicode This is the 0.5 documentation I'm using SQLAlchemy version 0.3.10. and cx_Oracle 4.3.1 Seems cx_Oracle isn't able to convert unicode values into integer. Take a look: In [32]: print Azienda.get(1).azienda_id 1 In [33]: print Azienda.id(u'1').azienda_id SQLError: (NotSupportedError) Variable_TypeByValue(): unhandled data type unicode 'SELECT azienda.id AS azienda_id \nFROM azienda \nWHERE azienda.id = :azienda_id ORDER BY azienda.rowid' {'azienda_id': u'1'} 0.3 has the Unicode type as well, the same usage rules apply. However in this case you are supplying a string for an integer value in a query. you have to supply an int, not a string. Yes, I'm supplying a value directly from the url which is a text value as in: www.myserver/azienda?id=1 I haven't this problem using the postgres adapter because psycopg casting automatically the value, instead cx_Oracle needs an explicit casting. I wonder if there's some parameter to tell cx_Oracle to do it in implict way. j --~--~-~--~~~---~--~~ 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] mssql unit tests how?
Hello, During 0.4.6 I was getting some help on mssql via linux, and it was expressed to me that it would be nice to run unit tests of sqlalchemy on mssql. I've just got new sql server 2005 installed if you guys tell me exactly (copy paste) what do I need to do to run these tests, I will run them. Let me know. Lucas -- How to create python package? http://lucasmanual.com/mywiki/PythonPaste Bazaar and Launchpad http://lucasmanual.com/mywiki/Bazaar --~--~-~--~~~---~--~~ 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: sqlite: copy from one database to another
Hi, forced to eating my dogfood after some time and found i need something better, here is the better version -- looks complicated, but i cant see better.. class CommonDatabase(object): def duplicateToDisk(self, target_file): connection = self.engine.connect() #this is in my instance original_meta = self.metadata #as well new_meta = MetaData() if os.path.exists(target_file): os.remove(target_file) try: connection.execute(attach '%s' as extern % target_file) #make a copy of metadata for table in original_meta.tables.values(): table.tometadata(new_meta) #set the scheme, important - and delete indexes (is there a better way?) for table in new_meta.tables.values(): table.schema = 'extern' del(table.indexes) table.create(connection) #copy content for table in original_meta.tables.values(): connection.execute(insert into extern.%s select * from %s % (table, table)) connection.execute(detach extern) #connect to it and recreate indexes engine = create_engine('sqlite:///%s' % target_file, echo=True) for t in original_meta.tables.values(): for ix in t.indexes: ix.create(bind=engine) except Exception, e: log.error(Exception duplicating database to file: %s % target_file) log.traceback() On Oct 20, 8:24 pm, rca roman.ch...@gmail.com wrote: Hmm :) I am looking for the same thing, I used to do it with sql, but I hoped to make it more orm Here is how i do it for sqlite defduplicateToDisk(self, file): '''Tohle ulozi databazi, ktera byla pouze v pameti, na disk''' cur = self.connection() import os if os.path.exists(file): os.remove(file) cur.execute(attach %s as extern % file) self.checkTable('extern.dictionary') cur.execute(insert into extern.dictionary select * from dictionary) cur.execute(detach extern) self.commit() On Sep 17, 8:51 pm, Doug Farrell dfarr...@mypublisher.com wrote: Hi all, I'm using SqlAlchemy with Sqlite to maintain a dynamic state table for a constantly running process. The table for this is kept in memory using the create_engine('sqlite:///:memory:') function call. My process is an endlessly running loop (with a sleep state), and I'd like to read the databse in from a disk file into the memory structure, process through my loop, and then write the state back out to the disk file, completely replacing the contents of the disk copy. Can anyone tell me, help me, advise me how I might go about doing this? I'm new to SqlAlchemy (noob), so forgive me if this is an dopey question. Thanks in advance, Doug --~--~-~--~~~---~--~~ 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] Session.merge changing related objects?
I am experiencing strange behavior in with Session.merge in 0.5.0rc4, where a flush cause merged objects on related entities to change object identity. I think this is a bug, but I might be missing something about Session.merge. (I've never used it before.) My test case can be viewed on pastebin at http://pastebin.com/m72d6885b (only TestMerge.test_merge_bug should fail). --~--~-~--~~~---~--~~ 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: Session.merge changing related objects?
I modified the test to run against UnitTest since I dont have nose installed, and all tests pass for me. My version is attached. Make sure you're actually running 0.5 since the test seems to be testing behavior that didn't work in older 0.4 versions of SQLA (although the other tests seem to be testing the same thing, so maybe thats not 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~--- from datetime import datetime, timedelta from sqlalchemy import create_engine, MetaData, Table, Column, ForeignKey from sqlalchemy.types import Integer, DateTime, Text from sqlalchemy.orm import mapper, relation from sqlalchemy.orm import scoped_session, sessionmaker import unittest def assert_identical (obj1, obj2): assert obj1 is obj2, %r is not %r % (obj1, obj2) def assert_equal(x, y): assert x== y metadata = MetaData() bars = Table(bars, metadata, Column(id, Integer, primary_key=True)) foos = Table(foos, metadata, Column(id, Integer, primary_key=True), Column(bar_id, None, ForeignKey(bars.id), nullable=False)) class Bar (object): def __init__ (self, id_): self.id = id_ class Foo (object): def __init__ (self, bar): self.id = None self.bar = bar mapper(Bar, bars, properties={ 'id':bars.c.id}) mapper(Foo, foos, properties={ 'id':foos.c.id, 'bar':relation(Bar, backref=foos)}) Session = sessionmaker() class TestMerge (unittest.TestCase): def setUp (self): metadata.bind = create_engine(sqlite:///:memory:, echo=True) metadata.create_all() def tearDown (self): metadata.drop_all() metadata.bind = None def test_normal_merge (self): s = Session() assert_identical(s.merge(Bar(1)), s.merge(Bar(1))) def test_merge_flush (self): s = Session() bar = s.merge(Bar(1)) before_id = id(bar) s.flush() after_id = id(bar) assert_equal(before_id, after_id) def test_merge_related_flush (self): s = Session() bar = s.merge(Bar(1)) before_id = id(bar) foo = Foo(bar) s.flush() after_id = id(bar) related_id = id(foo.bar) assert_equal(before_id, after_id) assert_equal(before_id, related_id) def test_double_merge_flush (self): s = Session() bar1 = s.merge(Bar(1)) bar2 = s.merge(Bar(1)) s.flush() assert_identical(bar1, bar2) def test_merge_bug (self): s = Session() foo1 = Foo(s.merge(Bar(1))) before_id = id(foo1.bar) foo2 = Foo(s.merge(Bar(1))) after_id = id(foo1.bar) other_id = id(foo2.bar) assert_equal(before_id, other_id) assert_equal(after_id, other_id) assert_equal(before_id, after_id) assert_identical(foo1.bar, foo2.bar) if __name__ == '__main__': unittest.main() On Dec 12, 2008, at 3:23 PM, Jonathon Anderson wrote: I am experiencing strange behavior in with Session.merge in 0.5.0rc4, where a flush cause merged objects on related entities to change object identity. I think this is a bug, but I might be missing something about Session.merge. (I've never used it before.) My test case can be viewed on pastebin at http://pastebin.com/ m72d6885b (only TestMerge.test_merge_bug should fail). --~--~-~--~~~---~--~~ 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: iterate_properties missing in 0.5?
On Thu, Dec 11, 2008 at 6:21 PM, Michael Bayer mike...@zzzcomputing.com wrote: On Dec 11, 2008, at 7:08 PM, Jorge Vargas wrote: Hi, has the behavior here http://www.sqlalchemy.org/trac/wiki/FAQ#Whatsthebestwaytofigureoutwhichattributesarecolumnsgivenaclass changed in 0.5? I'm trying that and getting AttributeError: iterate_properties this is my code, so far: klass = model.User def add_user(): obj = klass() this is the error: mapper = class_mapper(klass).columns you want the mapper, not mapper.columns ohhh I totally missed that from the other approach I had, which is a trick, I don't remember where I picked it up which I think only works for declarative. klass.__table__.columns thank you --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---