[sqlalchemy] how to get duplicated Key Error?
any code snippets? thanks. --~--~-~--~~~---~--~~ 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: how to get duplicated Key Error?
Dnia 2009-10-02, Pt o godzinie 21:22 -0700, 李白,字一日 pisze: any code snippets? thanks. How to handle unique constraint exception? http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueConstraintExceptionHandling --- Tomasz Jezierski Tefnet www.tefnet.pl --~--~-~--~~~---~--~~ 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: support for set/replace on a collection in AttributeExtension
On Oct 3, 2009, at 7:53 AM, Tomasz Jezierski - Tefnet wrote: if it were me, I'd just constrain access to a set of methods, such as: def set_col_attr(*values) We don't like polluting our namespace with useless get/set_col_attr etc... its useful depending on your use case.I have a model here which wants to receive a whole list of items at once, and its an explicit method - since thats the one way to get items into that particular collection. If I tried to instrument behind the scenes with event handlers and whatnot and attempted to flatten foo.colllection.append() and foo.collection = bar into the same system it would be a huge mess - its the definition of explciit is better than implicit. But that's my case, not yours. In this case I went back to the original email here and looked at what you're trying to accomplish.It seems like you are just trying to create an immutable collection. with descriptors it will be a problem with .append .remove methods (afaik descriptor will give us only set/get/del) so as I said you'd want to implement events for the collection as well. As for events on the collection itself you can still use attributeextension or alternatively a custom collection class. We wanted to create custom event for our attributeextension but we don't know how to pass our impl_class to attributes.register_attribute_impl so that we could use our extended CollectionAttributeImpl (with modified _set_iterable). Could you suggest the best way to pass our CollectionAttributeImpl to register_attribute_impl or maybe you can modify SQLA and allow to pass impl_class like it is done right now with collection_class. none of that should be needed - in this case, you're just trying to build an immutable collection, so collection_class is the most appropriate way to acheive this. See the docs at http://www.sqlalchemy.org/docs/05/mappers.html#custom-collection-implementations .You can elect a private method to be the @collection.appender that the ORM will use when populating the collection from the database. All pubilc mutator methods will raise an immutable exception. --~--~-~--~~~---~--~~ 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: padding of CHAR fields, inconsistent where clause; Oracle example
On Oct 2, 2009, at 6:05 PM, volx wrote: My module is called sandbox.py After importing it to ipython and letting it run, here's what I get for the test you suggested: In [47]: (sandbox.price_sources.c.desciption=='EJV').right.type Out[47]: OracleChar(length=100, convert_unicode=False, assert_unicode=None) The trouble that for some reason the code goes into sqlalchemy.types.String.get_dbapi_type instead of sqlalchemy.databases.oracle.OracleChar I added an _OracleChar type to the oracle dialect in trunk (0.6) which provides dbapi.FIXED_CHAR. I was able to reproduce your original behavior, added a unit test which exercises the select lookup on the CHAR column both with a straight Table as well as a reflected one, and it all passes. There was no issue with the get_dbapi_type() mechanism. That is in r6386. test: m = MetaData(testing.db) t = Table('t1', m, Column('id', Integer, primary_key=True), Column('data', CHAR(30), nullable=False) ) t.create() try: t.insert().execute( dict(id=1, data=value 1), dict(id=2, data=value 2), dict(id=3, data=value 3) ) eq_(t.select().where(t.c.data=='value 2').execute ().fetchall(), [(2, 'value 2 ')] ) m2 = MetaData(testing.db) t2 = Table('t1', m2, autoload=True) assert type(t2.c.data.type) is CHAR eq_(t2.select().where(t2.c.data=='value 2').execute ().fetchall(), [(2, 'value 2 ')] ) finally: t.drop() In [14]: pdb.run('sandbox.test()') string(1)module() (Pdb) b /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5- py2.6.egg/sqlalchemy/engine/default.py:322 Breakpoint 7 at /usr/local/lib/python2.6/site-packages/ SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/engine/default.py:322 (Pdb) c 2009-10-02 17:58:18,020 INFO sqlalchemy.engine.base.Engine.0x...97ec SELECT USER FROM DUAL 2009-10-02 17:58:18,020 INFO sqlalchemy.engine.base.Engine.0x...97ec {} 2009-10-02 17:58:18,025 INFO sqlalchemy.engine.base.Engine.0x...97ec select table_name from all_tables where table_name=:name and owner=:schema_name 2009-10-02 17:58:18,026 INFO sqlalchemy.engine.base.Engine.0x...97ec {'name': 'VICTOR_PRICE_SOURCES', 'schema_name': 'CDOCOLLAT'} /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/engine/default.py(322)set_input_sizes() - dbtype = typeengine.dialect_impl(self.dialect).get_dbapi_type (self.dialect.dbapi) (Pdb) dbtype *** NameError: name 'dbtype' is not defined (Pdb) typeengine.dialect_impl(self.dialect) OracleChar(length=100, convert_unicode=False, assert_unicode=None) (Pdb) # we should be going into OracleChar.get_dbapi_type *** SyntaxError: unexpected EOF while parsing (stdin, line 1) (Pdb) s --Call-- /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/types.py(124)dialect_impl() - def dialect_impl(self, dialect, **kwargs): (Pdb) l 119 Column('data', MyType(16)) 120 ) 121 122 123 124 - def dialect_impl(self, dialect, **kwargs): 125 try: 126 return self._impl_dict[dialect] 127 except AttributeError: 128 self._impl_dict = weakref.WeakKeyDictionary() # will be optimized in 0.6 129 return self._impl_dict.setdefault(dialect, dialect.type_descriptor(self)) (Pdb) l 130 except KeyError: 131 return self._impl_dict.setdefault(dialect, dialect.type_descriptor(self)) 132 133 def __getstate__(self): 134 d = self.__dict__.copy() 135 d.pop('_impl_dict', None) 136 return d 137 138 def get_col_spec(self): 139 Return the DDL representation for this type. 140 raise NotImplementedError() (Pdb) dialect sqlalchemy.databases.oracle.OracleDialect object at 0xa1ae54c (Pdb) n /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/types.py(125)dialect_impl() - try: (Pdb) n /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/types.py(126)dialect_impl() - return self._impl_dict[dialect] (Pdb) n --Return-- /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/types.py(126)dialect_impl()-OracleCh...ode=None) - return self._impl_dict[dialect] (Pdb) l 121 122 123 124 def dialect_impl(self, dialect, **kwargs): 125 try: 126 - return self._impl_dict[dialect] 127 except AttributeError: 128 self._impl_dict = weakref.WeakKeyDictionary() # will be optimized in 0.6 129 return self._impl_dict.setdefault(dialect,
[sqlalchemy] Re: padding of CHAR fields, inconsistent where clause; Oracle example
Hi Mike: Thank you for that. I will try it out on Monday. I see there is a major refactoring around types in version 0.6. One would be expected to define table classes with generic SQL types like CHAR or generic language types like String and not with dialect implementations like OracleChar. Am I right in that? Still it would be nice understand why the monkey patch did not work but it is probably not the best use of your time or mine. Thanks again for the prompt fix and I will be upgrading to 0.6. V. On Oct 3, 4:02 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Oct 2, 2009, at 6:05 PM, volx wrote: My module is called sandbox.py After importing it to ipython and letting it run, here's what I get for the test you suggested: In [47]: (sandbox.price_sources.c.desciption=='EJV').right.type Out[47]: OracleChar(length=100, convert_unicode=False, assert_unicode=None) The trouble that for some reason the code goes into sqlalchemy.types.String.get_dbapi_type instead of sqlalchemy.databases.oracle.OracleChar I added an _OracleChar type to the oracle dialect in trunk (0.6) which provides dbapi.FIXED_CHAR. I was able to reproduce your original behavior, added a unit test which exercises the select lookup on the CHAR column both with a straight Table as well as a reflected one, and it all passes. There was no issue with the get_dbapi_type() mechanism. That is in r6386. test: m = MetaData(testing.db) t = Table('t1', m, Column('id', Integer, primary_key=True), Column('data', CHAR(30), nullable=False) ) t.create() try: t.insert().execute( dict(id=1, data=value 1), dict(id=2, data=value 2), dict(id=3, data=value 3) ) eq_(t.select().where(t.c.data=='value 2').execute ().fetchall(), [(2, 'value 2 ')] ) m2 = MetaData(testing.db) t2 = Table('t1', m2, autoload=True) assert type(t2.c.data.type) is CHAR eq_(t2.select().where(t2.c.data=='value 2').execute ().fetchall(), [(2, 'value 2 ')] ) finally: t.drop() In [14]: pdb.run('sandbox.test()') string(1)module() (Pdb) b /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5- py2.6.egg/sqlalchemy/engine/default.py:322 Breakpoint 7 at /usr/local/lib/python2.6/site-packages/ SQLAlchemy-0.5.5-py2.6.egg/sqlalchemy/engine/default.py:322 (Pdb) c 2009-10-02 17:58:18,020 INFO sqlalchemy.engine.base.Engine.0x...97ec SELECT USER FROM DUAL 2009-10-02 17:58:18,020 INFO sqlalchemy.engine.base.Engine.0x...97ec {} 2009-10-02 17:58:18,025 INFO sqlalchemy.engine.base.Engine.0x...97ec select table_name from all_tables where table_name=:name and owner=:schema_name 2009-10-02 17:58:18,026 INFO sqlalchemy.engine.base.Engine.0x...97ec {'name': 'VICTOR_PRICE_SOURCES', 'schema_name': 'CDOCOLLAT'} /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/engine/default.py(322)set_input_sizes() - dbtype = typeengine.dialect_impl(self.dialect).get_dbapi_type (self.dialect.dbapi) (Pdb) dbtype *** NameError: name 'dbtype' is not defined (Pdb) typeengine.dialect_impl(self.dialect) OracleChar(length=100, convert_unicode=False, assert_unicode=None) (Pdb) # we should be going into OracleChar.get_dbapi_type *** SyntaxError: unexpected EOF while parsing (stdin, line 1) (Pdb) s --Call-- /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/types.py(124)dialect_impl() - def dialect_impl(self, dialect, **kwargs): (Pdb) l 119 Column('data', MyType(16)) 120 ) 121 122 123 124 - def dialect_impl(self, dialect, **kwargs): 125 try: 126 return self._impl_dict[dialect] 127 except AttributeError: 128 self._impl_dict = weakref.WeakKeyDictionary() # will be optimized in 0.6 129 return self._impl_dict.setdefault(dialect, dialect.type_descriptor(self)) (Pdb) l 130 except KeyError: 131 return self._impl_dict.setdefault(dialect, dialect.type_descriptor(self)) 132 133 def __getstate__(self): 134 d = self.__dict__.copy() 135 d.pop('_impl_dict', None) 136 return d 137 138 def get_col_spec(self): 139 Return the DDL representation for this type. 140 raise NotImplementedError() (Pdb) dialect sqlalchemy.databases.oracle.OracleDialect object at 0xa1ae54c (Pdb) n /usr/local/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg/ sqlalchemy/types.py(125)dialect_impl() - try: (Pdb) n