'AutogenContext' object has no attribute '__getitem__'
Hi, Looks like the autogen_context parameter has changed type recently. I'm getting this exception: Traceback (most recent call last): File "/home/fayaz/Programming/weaver-env/bin/weaver", line 9, in load_entry_point('weaver==2.12.1', 'console_scripts', 'weaver')() File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py", line 700, in __call__ return self.main(*args, **kwargs) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py", line 680, in main rv = self.invoke(ctx) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py", line 1027, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py", line 1027, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py", line 873, in invoke return ctx.invoke(self.callback, **ctx.params) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/core.py", line 508, in invoke return callback(*args, **kwargs) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/click/decorators.py", line 16, in new_func return f(get_current_context(), *args, **kwargs) File "/home/fayaz/Programming/weaver-backend/weaver/cli.py", line 44, in script weaver.db.generate_migrate_script(url=ctx.obj, message=message) File "/home/fayaz/Programming/weaver-backend/weaver/db/sql/__init__.py", line 132, in generate_migrate_script make_config(url), message, autogenerate=True) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/command.py", line 121, in revision revision_context.generate_scripts() File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 412, in generate_scripts yield self._to_script(generated_revision) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 336, in _to_script autogen_context, migration_script, template_args File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 40, in _render_python_into_templatevars _render_cmd_body(upgrade_ops, autogen_context)) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 63, in _render_cmd_body lines = render_op(autogen_context, op) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 75, in render_op lines = util.to_list(renderer(autogen_context, op)) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 102, in _render_modify_table t_lines = render_op(autogen_context, t_op) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 75, in render_op lines = util.to_list(renderer(autogen_context, op)) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 299, in _add_column "column": _render_column(column, autogen_context), File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 542, in _render_column 'type': _repr_type(column.type, autogen_context), File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 565, in _repr_type rendered = _user_defined_render("type", type_, autogen_context) File "/home/fayaz/Programming/weaver-env/local/lib/python2.7/site-packages/alembic/autogenerate/render.py", line 513, in _user_defined_render rendered = render(type_, object_, autogen_context) File "/home/fayaz/Programming/weaver-backend/weaver/db/migrations/env.py", line 52, in render_item autogen_context['imports'].add( Here's the code: def render_item(type_, obj, autogen_context): from weaver.core.model import PriceType if type_ == 'type': if isinstance(obj, PriceType): print autogen_context autogen_context['imports'].add( 'from weaver.core.model import PriceType') return '%r' % obj return False -- You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Global variables from inside render_item
Oh, I can reproduce the error without globals too. Here: from sqlalchemy.dialects import mysql def render_item(type_, obj, autogen_context): print "This is a global:", mysql from sqlalchemy.dialects import mysql as mysql2 print "This is not a global:", mysql2 Output: This is a global: None This is not a global: On Wednesday, September 2, 2015 at 8:53:21 PM UTC+5:30, Michael Bayer wrote: > > > > On 9/2/15 3:13 AM, Fayaz Yusuf Khan wrote: > > Hi, > I'm seeing this weird issue where several global variables in env.py are > set as null when accessed from render_item. > > Code: env.py > def render_item(type_, obj, autogen_context): > print globals() > > > I'm not deeply familiar with the vagaries of what globals() does other > than I tend not to go near it except when doing eval / exec, so I don't > have any immediate insight on this one, sorry (as always, I'd pdb it here). > > > > > Output: > {'mysql': None, 'with_statement': None, 'PasswordType': None, > 'include_symbol': None, 'PriceType': None, 'JSONEncodedDict': None, > 'compare_type': None, 'PhoneNumberType': None, 'run_migrations_online': > None, '__package__': None, 'render_item': None, 'target_metadata': None, > 'Base': None, 'config': None, '__doc__': None, '__builtins__': > {'bytearray': , 'IndexError': 'exceptions.IndexError'>, 'all': , 'help': Type > help() for interactive help, or help(object) for help about object., > 'var. > > Recently updated to: > alembic==0.8.2 > SQLAlchemy==1.0.8 > > Any ideas why this might be happening? > > Thanks. > > > -- You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Support for pysqlcipher3
On Saturday 13 Jun 2015 7:42:51 AM Mike Bayer wrote: if that is the only test failing I wouldn't worry too hard.if you look at the test, you'll see this comment: # fails on newer versions of pysqlite due to unusual memory behvior # in pysqlite itself. background at: # http://thread.gmane.org/gmane.comp.python.db.pysqlite.user/2290 In fact there's some hardcoded logic to help with this test that is hardcoded to pysqlite, try this patch: --- a/test/aaa_profiling/test_memusage.py +++ b/test/aaa_profiling/test_memusage.py @@ -45,7 +45,8 @@ def profile_memory(maxtimes=50): # tests under 50 iterations and ideally about ten, so # just filter them out so that we get a flatline more quickly. -if testing.against(sqlite+pysqlite): +if testing.against(sqlite+pysqlite) or \ +testing.against(sqlite+pysqlcipher): return [o for o in gc.get_objects() if not isinstance(o, weakref.ref)] else: What about these? == short test summary info === FAIL test/dialect/test_sqlite.py::InsertTest::()::test_empty_insert_pk4 FAIL test/dialect/test_suite.py::ComponentReflectionTest_sqlite_pysqlcipher:: ()::test_get_table_names FAIL test/dialect/test_suite.py::ComponentReflectionTest_sqlite_pysqlcipher:: ()::test_get_table_names_fks FAIL test/dialect/test_suite.py::ComponentReflectionTest_sqlite_pysqlcipher:: ()::test_get_tables_and_views FAIL test/engine/test_reflection.py::ReflectionTest_sqlite_pysqlcipher:: ()::test_reflect_all FAIL test/engine/test_reflection.py::ReflectionTest_sqlite_pysqlcipher:: ()::test_reflect_all_with_views FAIL test/engine/test_reflection.py::UnicodeReflectionTest_sqlite_pysqlcipher:: ()::test_basic FAIL test/engine/test_reflection.py::UnicodeReflectionTest_sqlite_pysqlcipher:: ()::test_get_names !! Interrupted: stopping after 25 failures !!! == 8 failed, 3115 passed, 611 skipped, 17 error in 1919.33 seconds === -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Support for pysqlcipher3
On Sunday 31 May 2015 11:36:12 PM Mike Bayer wrote: you can send in pysqlcipher3 to create_engine() using the dbapi argument: import pysqlcipher3 e = create_engine(sqlite+pysqlcipher:///file.db, dbapi=pysqlcipher3) feel free to submit a PR that returns this DBAPI from the dbapi method under py3k: So, I've been trying to make SQLiteDialect_pysqlcipher.dbapi return pysqlcipher3.dbapi2. But before I do that, I had to ensure all the tests were passing in the first place. But when I run the tests like this: py.test --dburi=sqlite+pysqlcipher://:test@/test.db -x I'm seeing: ... test/aaa_profiling/test_memusage.py::MemUsageTest_sqlite_pysqlcipher::test_fixture PASSED test/aaa_profiling/test_memusage.py::MemUsageTest_sqlite_pysqlcipher::test_join_cache FAILED == FAILURES == __ MemUsageTest_sqlite_pysqlcipher.test_join_cache ___ Traceback (most recent call last): File string, line 2, in test_join_cache File /home/fayaz/Programming/sqlalchemy/test/../lib/sqlalchemy/testing/exclusions.py, line 94, in decorate return self._do(config._current, fn, *args, **kw) File /home/fayaz/Programming/sqlalchemy/test/../lib/sqlalchemy/testing/exclusions.py, line 123, in _do self._expect_failure(config, ex, name=fn.__name__) File /home/fayaz/Programming/sqlalchemy/test/../lib/sqlalchemy/testing/exclusions.py, line 135, in _expect_failure util.raise_from_cause(ex) File /home/fayaz/Programming/sqlalchemy/test/../lib/sqlalchemy/util/compat.py, line 199, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) File /home/fayaz/Programming/sqlalchemy/test/../lib/sqlalchemy/testing/exclusions.py, line 121, in _do return_value = fn(*args, **kw) File /home/fayaz/Programming/sqlalchemy/test/aaa_profiling/test_memusage.py, line 732, in test_join_cache go() File /home/fayaz/Programming/sqlalchemy/test/aaa_profiling/test_memusage.py, line 96, in profile assert False, repr(samples) AssertionError: [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654, 227655, 227656, 227657, 227658, 227659, 227660, 227661, 227662, 227663, 227664, 227665, 227666, 227667, 227668, 227669, 227670, 227671, 227672, 227673, 227674, 227675, 227676, 227677, 227678, 227679] assert False Captured stdout call ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654, 227655, 227656, 227657, 227658, 227659]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654, 227655, 227656, 227657, 227658, 227659, 227660, 227661, 227662, 227663, 227664]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654, 227655, 227656, 227657, 227658, 227659, 227660, 227661, 227662, 227663, 227664, 227665, 227666, 227667, 227668, 227669]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654, 227655, 227656, 227657, 227658, 227659, 227660, 227661, 227662, 227663, 227664, 227665, 227666, 227667, 227668, 227669, 227670, 227671, 227672, 227673, 227674]) ('sample gc sizes:', [227630, 227631, 227632, 227633, 227634, 227635, 227636, 227637, 227638, 227639, 227640, 227641, 227642, 227643, 227644, 227645, 227646, 227647, 227648, 227649, 227650, 227651, 227652, 227653, 227654,
Re: [sqlalchemy] Support for pysqlcipher3
Alright, thanks! I would try to submit the PR. But it does look like a transitional package to me. How do we generally handle such upstream changes anyway? On Monday, June 1, 2015 at 9:06:20 AM UTC+5:30, Michael Bayer wrote: On 5/31/15 8:58 PM, Fayaz Yusuf Khan wrote: On Sunday 31 May 2015 11:06:32 AM Mike Bayer wrote: pysqlcipher is right here: http://docs.sqlalchemy.org/en/rel_1_0/dialects/sqlite.html#module-sqlalchemy .dialects.sqlite.pysqlcipher SQLAlchemy supports Python 2K and Python 3K in place though the above dialect has probably not been tested on Py3K. pysqlcipher does not support Py3K. https://github.com/leapcode/pysqlcipher/issues/3 It's a known issue. you can send in pysqlcipher3 to create_engine() using the dbapi argument: import pysqlcipher3 e = create_engine(sqlite+pysqlcipher:///file.db, dbapi=pysqlcipher3) feel free to submit a PR that returns this DBAPI from the dbapi method under py3k: https://bitbucket.org/zzzeek/sqlalchemy/src/0766c80b9c02fdbad3203835ab850ad690f4c03b/lib/sqlalchemy/dialects/sqlite/pysqlcipher.py?at=master#cl-82 -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Support for pysqlcipher3
On Sunday 31 May 2015 11:06:32 AM Mike Bayer wrote: pysqlcipher is right here: http://docs.sqlalchemy.org/en/rel_1_0/dialects/sqlite.html#module-sqlalchemy .dialects.sqlite.pysqlcipher SQLAlchemy supports Python 2K and Python 3K in place though the above dialect has probably not been tested on Py3K. pysqlcipher does not support Py3K. https://github.com/leapcode/pysqlcipher/issues/3 It's a known issue. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Support for pysqlcipher3
Hi, pysqlcipher3 is a Python3 port for pysqlcipher (for SQLite). Does SQLA currently have support for this? (I wasn't able to get it running for my project.) If not, any chances it will, in the future? Thanks. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Re: ...and MVC
On Tuesday, May 26, 2015 at 11:04:34 PM UTC+5:30, Jonathan Vanasco wrote: It is incredibly non-standard to have more than one transaction within a given request. It is also relatively non-standard to explicitly manage transactions in the application code. I do both of those (the former in very few circumstances) but am in a tiny minority of users. I do the former in cases which involve very long transactions. (Batch uploads and processing. I'm counting batch and background tasks as part of the controller layer here.) If you decide to handle session state yourself, you will need to address the intricacies of lazy-loading and collections. If a collection/attribute is not eager loaded, accessing it in a template will trigger a database query. If you have already closed the connection or session, sqlalchemy will reconnect and reload. I think they were talking about detaching objects from the session so that there's no way it'll reconnect with the database or make any further queries. I'm not saying I'd recommend it as it sounds... extreme. But what do I know? I usually only keep `session.commit()` calls in the controllers. Every other database operation is buried in the models and helper functions. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Declarative: defining relationship and column in one line
Hi, On Tuesday, May 19, 2015 at 7:41:45 PM UTC+5:30, Michael Bayer wrote: http://techspot.zzzeek.org/2011/05/17/magic-a-new-orm/ I had tried this one before but it looked like those columns were never added to the underlying table. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Hybrid comparator vs expression
If I'm writing a comparator decorator, does it take care of expression decorator too? Eg: @hybrid_property def phone_number(self): return self._phone_number @phone_number.comparator def phone_number(cls): Truncate the RHS value if it's too long to fit into the column return TruncatingComparator(cls._phone_number) @phone_number.expression ??? -- Fayaz Yusuf Khan Cloud architect, Dexetra SS, India fayaz.yusuf.khan_AT_gmail_DOT_com, fayaz_AT_dexetra_DOT_com +91-9746-830-823 -- 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: Bug: Inefficient query being generated by relationship, mixin class combo
Nice! Michael Bayer wrote: This use case is not fully functional until version 0.8, in 0.7 and earlier you'll see operations like join() and joinedload() failing to alias the user_hash=user_hash clause correctly.See http://www.sqlalchemy.org/trac/wiki/08Migration#Rewrittenrelationshipmechanics . -- Fayaz Yusuf Khan Cloud architect, Dexetra SS, India fayaz.yusuf.khan_AT_gmail_DOT_com, fayaz_AT_dexetra_DOT_com +91-9746-830-823 -- 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] Bug: Inefficient query being generated by relationship, mixin class combo
The attached script generates an inefficient query at the end: SELECT `People`.friday_id AS `People_friday_id`, `People`.parent_id AS `People_parent_id`, `People`.user_hash AS `People_user_hash` FROM `People` WHERE `People`.friday_id = %s AND `People`.user_hash = `People`.user_hash Please note the user_hash = user_hash clause being generated there. This query takes a heavy toll on the datastore and has become a huge bottleneck in our application. This is the core ORM configuration: class User(Base): hash = Column(String(64), primary_key=True) class UserMixin(object): @declared_attr def user_hash(cls): return Column(String(64), ForeignKey('User.hash'), primary_key=True) class People(UserMixin, Base): friday_id = Column(BigInteger, primary_key=True, nullable=False, autoincrement=False) # This line mysteriously fixes the query #user_hash = Column(String(64), ForeignKey('User.hash'), # primary_key=True) parent_id = Column(BigInteger) @declared_attr def parent(cls): return relationship( 'People', remote_side=[cls.friday_id, cls.user_hash], post_update=True) __table_args__ = ( ForeignKeyConstraint( ['parent_id', 'user_hash'], ['People.friday_id', 'People.user_hash'], ondelete='CASCADE'),) As shown in the comments, if I remove the UserMixin, the query becomes sane. -- Fayaz Yusuf Khan Cloud architect, Dexetra SS, India fayaz.yusuf.khan_AT_gmail_DOT_com, fayaz_AT_dexetra_DOT_com +91-9746-830-823 -- 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 sqlalchemy import * from sqlalchemy.orm import sessionmaker, backref, relationship from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy.ext.declarative import declarative_base, declared_attr class TableNameMixin(object): @declared_attr def __tablename__(cls): return cls.__name__ Base = declarative_base(cls=TableNameMixin) class User(Base): hash = Column(String(64), primary_key=True) class UserMixin(object): @declared_attr def user_hash(cls): return Column(String(64), ForeignKey('User.hash'), primary_key=True) class People(UserMixin, Base): friday_id = Column(BigInteger, primary_key=True, nullable=False, autoincrement=False) # This line mysteriously fixes the query #user_hash = Column(String(64), ForeignKey('User.hash'), primary_key=True) parent_id = Column(BigInteger) @declared_attr def parent(cls): return relationship( 'People', remote_side=[cls.friday_id, cls.user_hash], post_update=True) __table_args__ = ( ForeignKeyConstraint( ['parent_id', 'user_hash'], ['People.friday_id', 'People.user_hash'], ondelete='CASCADE'),) engine = create_engine('mysql://root@localhost', echo=True) engine.execute('CREATE DATABASE test') engine.execute('USE test') Session = sessionmaker() Session.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all() session = Session() hash_string = '0' * 64 session.add(User(hash=hash_string)) session.flush() session.add(People(user_hash='0'*64, friday_id=1, parent_id=1)) session.add(People(user_hash='0'*64, friday_id=2, parent_id=1)) people = session.query(People).filter_by(friday_id=2).one() print After query print people.parent engine.execute('DROP DATABASE test')
[sqlalchemy] Connection events don't work on connection objects?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 This code fails:- from sqlalchemy import event, create_engine def before_execute(conn, clauseelement, multiparams, params): log.info(Received statement: %s % clauseelement) engine = create_engine('mysql://root@localhost') connection = engine.connect() event.listen(connection, before_execute, before_execute) with:- Traceback (most recent call last): File test.py, line 8, in module event.listen(connection, before_execute, before_execute) File /usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.7-py2.7-linux- x86_64.egg/sqlalchemy/event.py, line 40, in listen (identifier,target)) sqlalchemy.exc.InvalidRequestError: No such event 'before_execute' for target 'sqlalchemy.engine.base.Connection object at 0x2a31450' - -- Fayaz Yusuf Khan Cloud architect, Dexetra SS, India fayaz.yusuf.khan_AT_gmail_DOT_com, fayaz_AT_dexetra_DOT_com +91-9746-830-823 -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (GNU/Linux) iQEcBAEBAgAGBQJP3ZvVAAoJEFp5PFlEQdUR6QMIAMyhJuHysLwCwKs3ilQ4HI7e 3RaAGIbd3XNoBCekC6knyvHSwkJImkxHxL28UQ4uJTpJtXMRY/YUqZMFiu9Owm7I BDyAudzOz/pSe5NkQGnvOxlcR0RFB/qkfDw+xTTGrUDxeGLSpEZfhrJyTkKEktdV RJLWba/JLsOq5tw0djxRT2oJ0LNADltVMRo+G8TpB45nRBLEY68exE8Zup/3od3d eHyiQQDD7ffz9MUzd+6YktC0vu30LNoj/1XkqzbFeBcZqq8CqkmfM2jQAWUheSlR sqiqBzPFDs4a+xxXl2iTlE2Syh1H0zxynhQbd2ldnl04UbZ1HClkDrh51OKcL2M= =M31b -END PGP SIGNATURE- -- 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.
Re: [sqlalchemy] A hybrid_property with the same name as the attribute
On Thursday 01 Mar 2012 1:27:30 PM Daniel Nouri wrote: Yes that's what I'm doing. My magic base class is my CMS's Node class. I like that it's easy for add-on authors to derive from it, and have the adjacency list etc. all set up already. Maybe I'll just make what seems to be the most common one to override, the 'title' attribute, underscore prefixed. That'll solve the problem at hand. This choice doesn't seem so extensible. Maybe, you should stick to the Special cases aren't special enough to break the rules. rule? import this I have learnt this the hard way. -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
[sqlalchemy] Delete failing with StaleDataError
The attached script fails with this: Traceback (most recent call last): File stale_delete.py, line 33, in module session.flush() File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py, line 1559, in flush self._flush(objects) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py, line 1630, in _flush flush_context.execute() File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py, line 331, in execute rec.execute(self) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py, line 498, in execute uow File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py, line 2507, in _delete_obj (table.description, len(del_objects), c.rowcount) sqlalchemy.orm.exc.StaleDataError: DELETE statement on table 'A' expected to delete 2 row(s); 1 were matched. in MySQL-InnoDB (works in SQLite and Postgres). Tried versions 0.7.3 and 0.7.5 Python 2.7 I tried manually deleting the rows from the table through MySQL client and noticed that it's not returning an accurate row count (doesn't 'supports_sane_rowcount') for a table with an adjacency relationship and an ondelete='CASCADE'. I'm going to work around this by adding another ondelete='CASCADE' for User-A -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, relationship, backref Base = declarative_base(bind=create_engine('mysql://root@localhost/test')) class User(Base): __tablename__ = 'User' id = Column(Integer, primary_key=True) class A(Base): __tablename__ = 'A' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey(User.id)) user = relationship(User, backref=backref( 'a', cascade='all, delete, delete-orphan')) parent_id = Column(Integer, ForeignKey('A.id', ondelete='CASCADE')) Base.metadata.create_all() session = sessionmaker()() user = User() session.add(user) session.add_all([A(id=1, parent_id=1, user=user), A(id=2, parent_id=1, user=user)]) session.flush() session.delete(user) session.flush() signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Delete failing with StaleDataError
On Saturday 11 Feb 2012 10:32:12 AM Michael Bayer wrote: I ran this and indeed InnoDB appears to be broken here. This is a MySQL bug. It would appear it is running the cascade between the two A rows and only considering the lead object to be the one deleted.Can't exactly find it at bugs.mysql.com either, so you'd do everyone a favor if you could create a ticket over there. I've raised this on lists.mysql. SQLAlchemy can try to work around this but for now you can just flip supports_sane_rowcount off for the whole dialect: Thanks, that would be a lot better than writing a new migrate script. -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] SQLAlchemy 0.7.5 Released
On Saturday 28 Jan 2012 6:05:51 PM Michael Bayer wrote: SQLAlchemy 0.7.5 is now available. Where's the documentation (pdf)? Or has nothing changed there since 0.7.4? http://media.readthedocs.org/pdf/sqlalchemy/latest/sqlalchemy.pdf still gives the older pdf. -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
[sqlalchemy] New error in table inheritance in 0.7.4
This simple inheritance mapping script (attached) would work in version 0.7.3 but not in 0.7.4. In the new version it would raise an ArgumentError: Traceback (most recent call last): File inheritance_discriminator.py, line 22, in module class PhoneContactTimeline(A): File /usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py, line 1273, in __init__ _as_declarative(cls, classname, cls.__dict__) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py, line 1266, in _as_declarative **mapper_args) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/__init__.py, line 1114, in mapper return Mapper(class_, local_table, *args, **params) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py, line 202, in __init__ self._configure_polymorphic_setter() File /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py, line 902, in _configure_polymorphic_setter Only direct column-mapped sqlalchemy.exc.ArgumentError: Only direct column-mapped property or SQL expression can be passed for polymorphic_on And it goes away if a replace cls.discriminator with a string 'discriminator'. The error persists for single table inheritance too. You can see that I'm using Python 2.7(.2) -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base, declared_attr Base = declarative_base() class A(Base): __tablename__ = 'A' id = Column(BigInteger, primary_key=True) discriminator = Column(String) @declared_attr def __mapper_args__(cls): mapper_args = {'polymorphic_identity': cls.__name__, 'polymorphic_on': cls.discriminator} # if cls has this_thing: mapper_args['some key'] = this_flag return mapper_args class PhoneContactTimeline(A): __tablename__ = 'B' id = Column(ForeignKey(A.id), primary_key=True) signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy]Close database connection in SQLAlchemy
On Sunday 22 Jan 2012 12:57:33 AM Sana wrote: • On page load im displaying data from the database each time the page is refreshed the query hits the database to retrieve the data there by taking long time for the page to load is thr any way by which the query hit the table oly for new request and for the same request access data from the cache There's a beaker caching example in SQLA. http://docs.sqlalchemy.org/en/latest/orm/examples.html#beaker-caching I myself am looking forward to implement this pattern using memcached in my own app. -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
[sqlalchemy] Enhancement request: Let __table_args__ accept empty tuples
As of now, the attached dummy script would fail with an IndexError as sqlalchemy is depending on the fact that __table_args__ is a non-empty tuple (if it is a tuple in the first place). However, there are some use cases (mostly involving mixins and inheritance) where it would be nice to allow a class to return an empty tuple instead of a NoneType. I have attached a trivial patch for sqlalchemy which checks for the boolean tautology of the table_args variable before extracting args and table_kw from it. And it doesn't seem to break anything. Looking forward to your feedback. -- Fayaz Yusuf Khan Cloud developer and architect Dexetra SS, Bangalore, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823diff -r 2b66b5abf755 lib/sqlalchemy/ext/declarative.py --- a/lib/sqlalchemy/ext/declarative.py Thu Dec 01 14:21:43 2011 -0500 +++ b/lib/sqlalchemy/ext/declarative.py Sat Dec 03 15:05:39 2011 +0530 @@ -1153,15 +1153,15 @@ if '__table__' not in dict_: if tablename is not None: -if isinstance(table_args, dict): -args, table_kw = (), table_args -elif isinstance(table_args, tuple): -if isinstance(table_args[-1], dict): -args, table_kw = table_args[0:-1], table_args[-1] -else: -args, table_kw = table_args, {} -else: -args, table_kw = (), {} +args, table_kw = (), {} +if table_args: +if isinstance(table_args, dict): +table_kw = table_args +elif isinstance(table_args, tuple): +if isinstance(table_args[-1], dict): +args, table_kw = table_args[0:-1], table_args[-1] +else: +args = table_args autoload = dict_.get('__autoload__') if autoload:from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base, declared_attr Base = declarative_base() class MyTable(Base): __tablename__ = 'table' id = Column(Integer, primary_key=True) @declared_attr def __table_args__(cls): return tuple() signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] ondelete Cascade function not working in sqlalchemy
On Friday 01 Jul 2011 7:42:18 PM Kartik Lakhotia wrote: I have described my tables declaratively with foreign key being used at many places. I did set onupdate and ondelete as Cascade but it is not working properly. It does not delete the values in the foreign key column when i delete that entry in primary key column. Even the restrict is also not working. Can you provide example test code of what you were trying to acheive? We may be able to work from there. -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] generic_repr for ImplicitForeignKeyConstraint
Hi, I'm trying to write a good __repr__ for this subclass. On reading your code, I saw that several of your __repr__ definitions depend on sqlalchemy.util.generic_repr (and several don't). Is it a public API? Don't see much documentation for it anywhere. Could you explain about its proper usage? On Monday 08 Aug 2011 12:17:16 AM Michael Bayer wrote: Sorry I didn't look closely. Yes when you subclass FKC overriding _set_parent() to perform additional steps is one way to establish on-connect details. Another is to use event.listen(ImplicitForeignKeyConstraint, after_parent_attach), it would be a little more of the public API. -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] discriminator_on_association.py - creating a Customer instance without specifying the addresses attribute
On Monday, June 27, 2011 03:55:04 PM Yap Sok Ann wrote: Using the discriminator_on_association.py example, if I create a Customer instance without specifying the addresses attribute, the for- loop at the end will throw exception: AttributeError: 'NoneType' object has no attribute 'addresses' It works if I specify addresses=[], but that sounds like too much work :P Is there a workaround for this? Thanks. You could simply override the __init__ method and pass a default argument. That would simplify your object constructions... Hope that helps. -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
[sqlalchemy] Feature request: Multilevel inheritance mapping
I was trying to configure a multilevel inheritance mapping (either using joint- table or single-table) but it didn't work as I was expecting it to. I understand that it probably has never been thought of, but in my particular use case, it would have been really handy. Any idea if this is possible||makes sense? -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Need for ImplicitForeignKeyConstraint
So IFKC(ImplicitForeignKeyConstraint) should not have inherited FKC? I did it so that it could seamlessly be passed into Table() and __table_args__. PS: Everyone, the repo is at https://bitbucket.org/fayaz/implicit On Friday, August 05, 2011 07:31:44 PM Michael Bayer wrote: yeah wow I just saw that.Can you use table.add_constraint(fk) instead of _set_parent() ? On Aug 5, 2011, at 1:34 AM, Fayaz Yusuf Khan wrote: So I had been working on this tiny project now and then. And here's the poc. http://paste.pound-python.org/show/10578/ I think I'm somewhat misusing the _set_parent() here though. On Sunday, July 24, 2011 06:52:45 PM Michael Bayer wrote: On Jul 24, 2011, at 8:39 AM, Fayaz Yusuf Khan wrote: The problem with using different mixins is that you lose out on a lot of code reusability. In my case, I have a 'user' column that appears in almost all table declarations. To have a separate mixin class for each joint-table inheritance would destroy the purpose of having a mixin altogether. In your example you can simply use CMixin and TMixin separately instead of inheriting them from one another, then apply CMixin and TMixin directly to C individually.That makes more sense here since for every class X which you want to have user, you'd apply CMixin explicitly. The more I look at this the more it seems completely correct to me. Mixins and declarative do a lot , and sticking to Python's regular rules for inheritance is what makes them great. Perhaps, there should be a shorthand for implicitly creating columns along with foreign key constraints? So something like ImplicitForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp'], primary_key=True) should lead to the creation of Column('user', String, primary_key=True), Column('timestamp',Integer, autoincrement=False, primary_key=True), ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']) Not something for core but certainly something you could provide yourself (use append_column()). SQLA's APIs try to remain explicit about things leaving implicit helper layers as an external task (hence relationship + ForeignKey, as opposed to the all in one demo I did at http://techspot.zzzeek.org/2011/05/17/magic-a-new-orm/ , etc) -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Need for ImplicitForeignKeyConstraint
So I had been working on this tiny project now and then. And here's the poc. http://paste.pound-python.org/show/10578/ I think I'm somewhat misusing the _set_parent() here though. On Sunday, July 24, 2011 06:52:45 PM Michael Bayer wrote: On Jul 24, 2011, at 8:39 AM, Fayaz Yusuf Khan wrote: The problem with using different mixins is that you lose out on a lot of code reusability. In my case, I have a 'user' column that appears in almost all table declarations. To have a separate mixin class for each joint-table inheritance would destroy the purpose of having a mixin altogether. In your example you can simply use CMixin and TMixin separately instead of inheriting them from one another, then apply CMixin and TMixin directly to C individually.That makes more sense here since for every class X which you want to have user, you'd apply CMixin explicitly. The more I look at this the more it seems completely correct to me. Mixins and declarative do a lot , and sticking to Python's regular rules for inheritance is what makes them great. Perhaps, there should be a shorthand for implicitly creating columns along with foreign key constraints? So something like ImplicitForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp'], primary_key=True) should lead to the creation of Column('user', String, primary_key=True), Column('timestamp',Integer, autoincrement=False, primary_key=True), ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']) Not something for core but certainly something you could provide yourself (use append_column()). SQLA's APIs try to remain explicit about things leaving implicit helper layers as an external task (hence relationship + ForeignKey, as opposed to the all in one demo I did at http://techspot.zzzeek.org/2011/05/17/magic-a-new-orm/ , etc) -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Delay in getting the response object and populating data to UI page using Python, SQLAlchemy, MySQL
On Wednesday 15 Jun 2011 11:44:32 AM Bala wrote: def write(self, params): if 'id' in params: ticket = self.dao.FindByID(Ticket, params['id'][0]) ticket.modified = datetime.datetime.now() else: ticket = Ticket() # Mark ticket open ticket.statusId = 1 # TODO: Make this line more robust in case status indexes change if 'deptId' in params: ticket.deptId = params['deptId'][0] if 'cityId' in params: ticket.cityId = params['cityId'][0] if 'creatorId' in params: ticket.creatorId= params['creatorId'][0] if 'title' in params: ticket.title= cgi.escape(params['title'][0]) if 'body' in params: ticket.body = cgi.escape(params['body'][0]) if 'severityId' in params: ticket.severityId = params['severityId'][0] if 'priorityId' in params: ticket.priorityId = params['priorityId'][0] if 'statusId' in params: ticket.statusId = params['statusId'][0] if 'project_id' in params: ticket.project_id = params['project_id'][0] if 'workstation_ip' in params: ticket.workstation_ip = params['workstation_ip'][0] if 'stateId' in params: stateId = params['stateId'][0] ticket.stateId = None if stateId == -1 else stateId if 'ownerId' in params: #ownerId = int(params['ownerId'][0]) ownerId = params['ownerId'][0] ticket.ownerId = None if ownerId == -1 else ownerId if 'interested[]' in params: self.clearInterested(ticket) for email in params['interested[]']: interested = InterestedParty(cgi.escape(email)) ticket.interested.append(interested) if 'interested' in params: self.clearInterested(ticket) for email in (params['interested'][0]).split(,): email = cgi.escape(email.strip()) # EMail might be blank, so check for that if email: email = InterestedParty(email) ticket.interested.append(email) self.dao.Update(ticket) self.dao.CommitSession() strModifications = Messages.messageHeader(ticket, params) strMessage = strModifications strMessage += Messages.ticketChanges(ticket, params) message = Message() message.ticket = ticket message.body= strMessage message.userId = params['userId'][0] # Save our addition/changes self.dao.Update(message) self.dao.CommitSession() userModifierName = message.user.name if ticket.owner == None: # Notify the mailing list since there is no owner sendTo = collectAllEMails(ticket) else: # If there's an owner, we don't need to tell everyone # what's happening sendTo = collectCreatorOwnerEMails(ticket) sender = t...@tete.com subject = Messages.getEMailSubject(ticket, params) body= Messages.getTicketEMailBody(ticket, params, userModifierName) Mailer.SendMail(subject, body, sender, sendTo) return ticket I believe you forgot to write a question? ;-) -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Need for ImplicitForeignKeyConstraint
On Saturday 23 Jul 2011 8:24:31 PM Michael Bayer wrote: On Jul 23, 2011, at 7:54 AM, Fayaz Yusuf Khan wrote: When C inherits from CMixin and Timeline, Python's method resolution is going to put TMixin after Timeline, that is: (class '__main__.C', class '__main__.CMixin', class '__main__.Timeline', class '__main__.TMixin', class 'sqlalchemy.ext.declarative.Base', class '__main__.TableName', type 'object') Maybe, joint-table inheritance and mixin syntaxes should be made more distinct and unambiguous? It may well be impossible (and backward incompatible) to implement in the current code-base, but a usage recipe which implements this behaviour might be possible. In my view this is the expected behavior of Python's class resolution. Declarative builds on top of this concept to produce mappings based polymorphic class behavior, which means the most class-specific attribute wins. If an attribute named X is a mapped column by the mapped parent, and that is the first __mro__ that the child class can see it, it follows that the child class should not have this column copied out to its own table unless the child specifies this attribute in some other way as to override it - else joined inheritance would not be possible to set up with @declared_attr, every @declared_attr would be copied out to all subclasses. It's easy to produce tests where the wrong behavior occurs, if I force declarative to hit the @declared_attr directly (I added some in the changeset I implemented regarding this). Perhaps a flag on @declared_attr, something like @declared_attr(final=True), meaning, use this @declared_attr directly in all cases regardless of what the class actually sees.That seems a little hard to explain. I think you should consider as I mentioned that your subclass columns and superclass columns of the same name aren't really the same, and just use different mixins for those two roles. The problem with using different mixins is that you lose out on a lot of code reusability. In my case, I have a 'user' column that appears in almost all table declarations. To have a separate mixin class for each joint-table inheritance would destroy the purpose of having a mixin altogether. Now, I'm thinking that messing with the mro may not be a good solution after all. Having a final argument for declared_attr seems unintuitive. Perhaps, there should be a shorthand for implicitly creating columns along with foreign key constraints? So something like ImplicitForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp'], primary_key=True) should lead to the creation of Column('user', String, primary_key=True), Column('timestamp',Integer, autoincrement=False, primary_key=True), ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']) ie., the column definitions should be fetched from the refcolumns. But I think some flags should be settable from this side too. Not sure about how flexible that can be made. -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Problem with class mixin hierarchy in joint table inheritance
On Thursday, July 21, 2011 09:27:21 PM Michael Bayer wrote: On Jul 21, 2011, at 10:48 AM, Michael Bayer wrote: On Jul 21, 2011, at 2:11 AM, Fayaz Yusuf Khan wrote: Hi, I have this declarative table model: http://paste.pound-python.org/show/9857/ The short answer is you can't do it that way right now. The user and timestamp columns of C can't get assigned to the Table by declarative because they've already been instrumented by the mapping of the Timeline class - the columns generated on the TMixin class are replaced by the instrumented versions of Timeline. Well, I'd really like to see this feature implemented. You'd have to forego the usage of mixins here or have C() use a mixin that isn't in the hierarchy of Timeline. Ticket #2226 is added to see if this can be fixed. OK it's an invalid, though I made the error message with the ForeignKeyConstraint nicer in the latest tip. When C inherits from CMixin and Timeline, Python's method resolution is going to put TMixin after Timeline, that is: (class '__main__.C', class '__main__.CMixin', class '__main__.Timeline', class '__main__.TMixin', class 'sqlalchemy.ext.declarative.Base', class '__main__.TableName', type 'object') Maybe, joint-table inheritance and mixin syntaxes should be made more distinct and unambiguous? It may well be impossible (and backward incompatible) to implement in the current code-base, but a usage recipe which implements this behaviour might be possible. The user and timestamp columns you're looking to be copied separately to both C and Timeline only get applied to Timeline - since Timeline is mapped and it ends the scope of TMixin propagating its attributes outward. A similar example shows how this behavior produces what's expected. Below, Child inherits from Parent, but we would expect Child to get the behavior of y() from Parent, not that of SomeMixin which is behind Parent: from sqlalchemy import Column, String, ForeignKey, Integer from sqlalchemy.ext.declarative import declarative_base, declared_attr class TableName(object): @declared_attr def __tablename__(cls): return cls.__name__ Base=declarative_base(cls=TableName) class SomeMixin(object): @declared_attr def x(cls): return Column(String) @declared_attr def y(cls): return Column(String) class Parent(SomeMixin, Base): id = Column(Integer, primary_key=True) def y(self): return hi class SomeOtherMixin(SomeMixin): pass # this: class Child(SomeOtherMixin, Parent): id = Column(Integer, ForeignKey('Parent.id'), primary_key=True) # is essentially the same here as: #class Child(Parent): # id = Column(Integer, ForeignKey('Parent.id'), primary_key=True) # Parent.y overrides the @declared_attr in SomeMixin assert Child().y() == 'hi' # so similarly, SomeMixin.x only applies to Parent assert 'x' not in Child.__table__.c # Child.x and Parent.x are the same mapped property, # ultimately. assert Child.x.property is Parent.x.property In your example, the user and timeline columns as associated with C are not really the same as the columns on Timeline - the Timeline columns are the ultimate autogenerating PK columns, whereas those of C are referencing - they have a different role. So it makes sense they be declared distinctly on CMixin. CMixin extends TMixin below but it might as well not, as Timeline in the middle takes the place of TMixin. from sqlalchemy import Column, String, ForeignKeyConstraint, Integer from sqlalchemy.ext.declarative import declarative_base, declared_attr class TableName(object): @declared_attr def __tablename__(cls): return cls.__name__ Base=declarative_base(cls=TableName) class TMixin(object): @declared_attr def user(cls): return Column(String, primary_key=True) @declared_attr def timestamp(cls): return Column(Integer, autoincrement=False, primary_key=True) class Timeline(TMixin, Base): snaptype = Column(Integer, primary_key=True, autoincrement=False) __mapper_args__ = {'polymorphic_on': snaptype} class CMixin(TMixin): @declared_attr def user(cls): return Column(String, primary_key=True) @declared_attr def timestamp(cls): return Column(Integer, autoincrement=False, primary_key=True) @declared_attr def __table_args__(cls): return (ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']),{}) @declared_attr def __mapper_args__(cls): return {'polymorphic_identity': CMixin.__subclasses__().index(cls)} class C(CMixin, Timeline): pass == from sqlalchemy import Column, String, ForeignKeyConstraint, Integer from sqlalchemy.ext.declarative import declarative_base, declared_attr class
[sqlalchemy] Problem with class mixin hierarchy in joint table inheritance
Hi, I have this declarative table model: http://paste.pound-python.org/show/9857/ == from sqlalchemy import Column, String, ForeignKeyConstraint, Integer from sqlalchemy.ext.declarative import declarative_base, declared_attr class TableName(object): @declared_attr def __tablename__(cls): return cls.__name__ Base=declarative_base(cls=TableName) class TMixin(object): @declared_attr def user(cls): return Column(String, primary_key=True) @declared_attr def timestamp(cls): return Column(Integer, autoincrement=False, primary_key=True) class Timeline(TMixin, Base): snaptype = Column(Integer, primary_key=True, autoincrement=False) __mapper_args__ = {'polymorphic_on': snaptype} class CMixin(TMixin): @declared_attr def __table_args__(cls): return (ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']),) @declared_attr def __mapper_args__(cls): return {'polymorphic_identity': CMixin.__subclasses__().index(cls)} class C(CMixin, Timeline): pass == But on running this code, I get: == Traceback (most recent call last): File schema.py, line 45, in module class C(CMixin, Timeline): File /usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py, line 1129, in __init__ _as_declarative(cls, classname, cls.__dict__) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py, line 1027, in _as_declarative **table_kw) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py, line 265, in __new__ table._init(name, metadata, *args, **kw) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py, line 340, in _init self._init_items(*args) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py, line 64, in _init_items item._set_parent_with_dispatch(self) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/events.py, line 227, in _set_parent_with_dispatch self._set_parent(parent) File /usr/local/lib/python2.7/dist-packages/sqlalchemy/schema.py, line 2010, in _set_parent col = table.c[col] File /usr/local/lib/python2.7/dist- packages/sqlalchemy/util/_collections.py, line 88, in __getitem__ return self._data[key] KeyError: 'user' == I don't get any error if C isn't inheriting from Timeline (ie, no joint-table inheritance). What could be wrong here? sqlalchemy.__version__ = '0.7.1' And thanks in advance. -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 from sqlalchemy import Column, String, ForeignKeyConstraint, Integer from sqlalchemy.ext.declarative import declarative_base, declared_attr class TableName(object): @declared_attr def __tablename__(cls): return cls.__name__ Base=declarative_base(cls=TableName) class TMixin(object): @declared_attr def user(cls): return Column(String, primary_key=True) @declared_attr def timestamp(cls): return Column(Integer, autoincrement=False, primary_key=True) class Timeline(TMixin, Base): snaptype = Column(Integer, primary_key=True, autoincrement=False) class CMixin(TMixin): @declared_attr def __table_args__(cls): return (ForeignKeyConstraint( ['user', 'timestamp'], ['Timeline.user', 'Timeline.timestamp']),) class C(CMixin, Base): pass signature.asc Description: This is a digitally signed message part.
Re: [sqlalchemy] Autocreate parent while inserting child
On Monday, June 20, 2011 07:34:42 PM Michael Bayer wrote: SQLA doesn't automatically create any objects so you'd need to create the Parent object yourself, but you'd also use relationship(): class Child(Base): parent_name = Column(String, ForeignKey('parent.name')) parent = relationship(Parent) def __init__(self, name): self.parent = Parent(name) relationship() would handle the parent_name assignment as well as adding Parent to the Session. I did that. But later on, when I had to insert a Child to an existing Parent, it raised IntegrityErrors once again. With some IRC help, I found a solution using the UniqueObject recipe (http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject). I was about to use that in my code, but then I saw that there's a method called session.merge(), which apears to do the same thing. Please correct me if I'm wrong. -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.
[sqlalchemy] Autocreate parent while inserting child
Hi, I'm a SQLA and MySQL noob. I have a many to one relationship somewhat similar to this: class Parent(Base): ... name = Column(String, primary_key=True) class Child(Base): ... parent = Column(String, ForeignKey('Parent.name')) And I'm trying to do this: session.add(Child(..., name='NewParent',...)) But on commit, it raises an IntegrityError. How can I make SQLA automatically insert a Parent in this case? -- Fayaz Yusuf Khan Cloud developer and designer Dexetra SS, Kochi, India fayaz.yusuf.khan_AT_gmail_DOT_com fayaz_AT_dexetra_DOT_com +91-9746-830-823 signature.asc Description: This is a digitally signed message part.