Re: [sqlalchemy] Not able to filter column by datetime
On 4/15/15 6:52 AM, Juan Antonio Ibáñez wrote: Hello, I have dozens of queries filtering DateTime columns but I have got one I don't know why it doesn't work. I have: --- q = DBSession.query(func.sum(RecargaCredito.importe), Local.nombre)\ .join((Local, RecargaCredito.locales_id == Local.id)) fl_desde = (datetime.now() - timedelta(days=7)).replace(hour=0, minute=0, second=0) fl_hasta = datetime.now().replace(hour=23, minute=59, second=59) q = q.filter(RecargaCredito.fechayhora = fl_desde).\ filter(RecargaCredito.fechayhora = fl_hasta) q = q.group_by(RecargaCredito.locales_id) f = q.all() --- RecargaCredito looks: --- class RecargaCredito(DeclarativeBase): __tablename__ = 'recargas_credito' __table_args__ = {'mysql_engine':'InnoDB'} id=Column(Integer, primary_key=True) fechayhora = DateTime importe = Column(DECIMAL(9,2)) locales_id=Column(Integer, ForeignKey('locales.id')) local = relation('Local', remote_side = 'Local.id') --- and I get error 'TypeError: can't compare datetime.datetime to VisitableType' you seem to be missing Column() for fechayhora: fechayhora = Column(DateTime) I've checked fl_desde and fl_hasta are python datetime objects Do you know what that error says? Regards -- 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 mailto:sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] polymorphic inheritance and unique constraints
hello Mike! so ... ok, based on this link http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#i-m-getting-a-warning-or-error-about-implicitly-combining-column-x-under-attribute-y (yeah yeah, well, rtfm for me), I was able to make it work like this: code class ContainerInstance(CoreMixin, TimestampMixin): container_instance_id = CoreMixin.column_id() parent_id = CoreMixin.column_fk(container_instance_id, nullable=False) batch_id = CoreMixin.column_fk(Batch.id_, nullable=False) container_instance_type = Column(EnumDictForInt(ContainerInstanceEnum), nullable=False, index=True) __mapper_args__ = { polymorphic_on: container_instance_type, polymorphic_identity: ContainerInstanceEnum.NONE } class ContainerAggregation(ContainerInstance): container_instance_id = CoreMixin.column_fk(ContainerInstance.id_, primary_key=True) container_descriptor_id = CoreMixin.column_fk(ContainerDescriptor.id_, nullable=False) # tada! batch_id = column_property(Column(BigInteger), ContainerInstance.batch_id) __mapper_args__ = { polymorphic_identity: ContainerInstanceEnum.AGGREGATION } UniqueConstraint(ContainerAggregation.container_descriptor_id, ContainerAggregation.batch_id) /code which brings me the question: I'm targeting *only* postgresql, so I have no need to pursue an agnostic approach in terms of inheritance. i do know that postgres inheritance system was discussed a lot in here, but, in my case - where i do want to have a constraint between parent and children - isn't it better to use postgres inheritance instead of duplicating the value to another table? well, i did notice the sqlalchemy example of postgres inheritance https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PostgreSQLInheritance, which uses written ddl and triggers. the problem is that i have extra fields in the inheritance table, which I think it is not a very good approach to postgres inheritance, but, either way, from your experience, what would be your tip? ps: i found this link http://postgresql.nabble.com/Constraint-to-ensure-value-does-NOT-exist-in-another-table-td4493651.html interesting and may be another solution to this, since i already have a table descriptor (the polymorphic_on expression). of course, the approach does require an extra table, but with events I can easily make it work in sqlalchemy. cheers, richard. On 04/14/2015 08:40 AM, Richard Gerd Kuesters | Pollux Automation wrote: here, a better illustration with my actual code: http://pastebin.com/RxS8Lzft best regards, richard. On 04/13/2015 06:30 PM, Mike Bayer wrote: On 4/13/15 4:59 PM, Richard Gerd Kuesters | Pollux Automation wrote: well, this didn't work with upstream 1.0 - sorry, I was in another project and couldn't test it myself. you're not doing the same thing this user was doing in any case... Traceback (most recent call last): File database_test.py, line 46, in module from plx.db.core import * File ../src/plx/db/core.py, line 901, in module UniqueConstraint(ContainerInstance.batch_id, ContainerAggregation.container_descriptor_id,) File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2464, in __init__ ColumnCollectionMixin.__init__(self, *columns, _autoattach=_autoattach) File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2393, in __init__ self._check_attach() File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2429, in _check_attach table.description) sqlalchemy.exc.ArgumentError: Column(s) 'container_aggregation.fk_container_descriptor_id' are not part of table 'container_instance'. I got sqlalchemy from git, today. sqlalchemy.__version__ '1.0.0' container_aggretation is a subclass of container_instance. I'm not using concrete inheritance here, may this be the problem? anything else, it's Python 2.7.9 + Linux + PostgreSQL 9.4.1. cheers, richard. On 03/24/2015 08:49 PM, Michael Bayer wrote: are these two separate constraints? I just looked and it seems like they are distinct. I just added a fix to 1.0 because someone was hacking around something similar to this. The easiest way to get these for the moment is just to create the UniqueConstraint outside of the class definition. class Foo(Base): # … class Bar(Foo): # … UniqueConstraint(Bar.x, Foo.y) that way all the columns are set up, should just work. Richard Gerd Kuesters | Polluxrich...@pollux.com.br wrote: well, understanding better the docs for column conflicts, can i use a declared_attr in a unique constraint? if yes, my problem is solved :) On 03/24/2015 10:33 AM, Michael Bayer wrote: Richard Gerd Kuesters | Pollux
Re: [sqlalchemy] polymorphic inheritance and unique constraints
nevermind. i'm again victim of rtfm :) http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#postgresql-table-options great work on this, btw. it'll simplify my life *A LOT* :) best regards, richard. On 04/15/2015 10:10 AM, Richard Gerd Kuesters wrote: hello Mike! so ... ok, based on this link http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#i-m-getting-a-warning-or-error-about-implicitly-combining-column-x-under-attribute-y (yeah yeah, well, rtfm for me), I was able to make it work like this: code class ContainerInstance(CoreMixin, TimestampMixin): container_instance_id = CoreMixin.column_id() parent_id = CoreMixin.column_fk(container_instance_id, nullable=False) batch_id = CoreMixin.column_fk(Batch.id_, nullable=False) container_instance_type = Column(EnumDictForInt(ContainerInstanceEnum), nullable=False, index=True) __mapper_args__ = { polymorphic_on: container_instance_type, polymorphic_identity: ContainerInstanceEnum.NONE } class ContainerAggregation(ContainerInstance): container_instance_id = CoreMixin.column_fk(ContainerInstance.id_, primary_key=True) container_descriptor_id = CoreMixin.column_fk(ContainerDescriptor.id_, nullable=False) # tada! batch_id = column_property(Column(BigInteger), ContainerInstance.batch_id) __mapper_args__ = { polymorphic_identity: ContainerInstanceEnum.AGGREGATION } UniqueConstraint(ContainerAggregation.container_descriptor_id, ContainerAggregation.batch_id) /code which brings me the question: I'm targeting *only* postgresql, so I have no need to pursue an agnostic approach in terms of inheritance. i do know that postgres inheritance system was discussed a lot in here, but, in my case - where i do want to have a constraint between parent and children - isn't it better to use postgres inheritance instead of duplicating the value to another table? well, i did notice the sqlalchemy example of postgres inheritance https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PostgreSQLInheritance, which uses written ddl and triggers. the problem is that i have extra fields in the inheritance table, which I think it is not a very good approach to postgres inheritance, but, either way, from your experience, what would be your tip? ps: i found this link http://postgresql.nabble.com/Constraint-to-ensure-value-does-NOT-exist-in-another-table-td4493651.html interesting and may be another solution to this, since i already have a table descriptor (the polymorphic_on expression). of course, the approach does require an extra table, but with events I can easily make it work in sqlalchemy. cheers, richard. On 04/14/2015 08:40 AM, Richard Gerd Kuesters | Pollux Automation wrote: here, a better illustration with my actual code: http://pastebin.com/RxS8Lzft best regards, richard. On 04/13/2015 06:30 PM, Mike Bayer wrote: On 4/13/15 4:59 PM, Richard Gerd Kuesters | Pollux Automation wrote: well, this didn't work with upstream 1.0 - sorry, I was in another project and couldn't test it myself. you're not doing the same thing this user was doing in any case... Traceback (most recent call last): File database_test.py, line 46, in module from plx.db.core import * File ../src/plx/db/core.py, line 901, in module UniqueConstraint(ContainerInstance.batch_id, ContainerAggregation.container_descriptor_id,) File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2464, in __init__ ColumnCollectionMixin.__init__(self, *columns, _autoattach=_autoattach) File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2393, in __init__ self._check_attach() File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2429, in _check_attach table.description) sqlalchemy.exc.ArgumentError: Column(s) 'container_aggregation.fk_container_descriptor_id' are not part of table 'container_instance'. I got sqlalchemy from git, today. sqlalchemy.__version__ '1.0.0' container_aggretation is a subclass of container_instance. I'm not using concrete inheritance here, may this be the problem? anything else, it's Python 2.7.9 + Linux + PostgreSQL 9.4.1. cheers, richard. On 03/24/2015 08:49 PM, Michael Bayer wrote: are these two separate constraints? I just looked and it seems like they are distinct. I just added a fix to 1.0 because someone was hacking around something similar to this. The easiest way to get these for the moment is just to create the UniqueConstraint outside of the class definition. class Foo(Base): # … class Bar(Foo): # … UniqueConstraint(Bar.x, Foo.y) that way all the columns are set up, should just work. Richard Gerd
Re: [sqlalchemy] polymorphic inheritance and unique constraints
oops, i forgot to comment out the fk to the parent table and now it doesn't work: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'container_instance' and 'container_aggregation'. well, it doesn't need it if it's inherited (both db and software level), right? On 04/15/2015 02:55 PM, Richard Gerd Kuesters wrote: the table definitions are listed here: http://pastebin.com/RxS8Lzft i'm using polymorphic associations, but with inheritance (INHERITS) there's no need to do it (imho), so the fk column to the parent table (which is also the pk) can be overriden. using add_is_dependent_on did the trick. i didn't know of such a feature ... thanks for bring it on :) although, is there a way to use it in declarative, intead of: MyModel.__table__.add_is_dependent_on(MyParentModel.__table__) ? cheers, richard. On 04/15/2015 02:44 PM, Mike Bayer wrote: On 4/15/15 1:07 PM, Richard Gerd Kuesters wrote: ok, now i have an issue. i don't know why, but sqlalchemy seems to issue the create table command of inherited postgresql tables before the base one in metadata.create_all(). commenting the inherited table, issuing create all, what do your table defs look like? The tables are created in order of FK dependency, and up until 1.0 there was no ordering beyond that. in 1.0 they will be by table name if there are no FK dependencies. if you're using this with INHERITS types of setups then you should establish which table is dependent on which using add_is_dependent_on(): http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html?highlight=add_is_dependent_on#sqlalchemy.schema.Table.add_is_dependent_on then uncomment the table and issuing create all again seems to work, but ... it's a heck of a workaround (if i think in terms of code). i even tried to use serializable isolation level, but no result. importing models in the desired order doesn't affect the behavior either. well, i'm out of options :) a little help? best regards, richard. On 04/15/2015 11:48 AM, Richard Gerd Kuesters wrote: nevermind. i'm again victim of rtfm :) http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#postgresql-table-options great work on this, btw. it'll simplify my life *A LOT* :) best regards, richard. On 04/15/2015 10:10 AM, Richard Gerd Kuesters wrote: hello Mike! so ... ok, based on this link http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#i-m-getting-a-warning-or-error-about-implicitly-combining-column-x-under-attribute-y (yeah yeah, well, rtfm for me), I was able to make it work like this: code class ContainerInstance(CoreMixin, TimestampMixin): container_instance_id = CoreMixin.column_id() parent_id = CoreMixin.column_fk(container_instance_id, nullable=False) batch_id = CoreMixin.column_fk(Batch.id_, nullable=False) container_instance_type = Column(EnumDictForInt(ContainerInstanceEnum), nullable=False, index=True) __mapper_args__ = { polymorphic_on: container_instance_type, polymorphic_identity: ContainerInstanceEnum.NONE } class ContainerAggregation(ContainerInstance): container_instance_id = CoreMixin.column_fk(ContainerInstance.id_, primary_key=True) container_descriptor_id = CoreMixin.column_fk(ContainerDescriptor.id_, nullable=False) # tada! batch_id = column_property(Column(BigInteger), ContainerInstance.batch_id) __mapper_args__ = { polymorphic_identity: ContainerInstanceEnum.AGGREGATION } UniqueConstraint(ContainerAggregation.container_descriptor_id, ContainerAggregation.batch_id) /code which brings me the question: I'm targeting *only* postgresql, so I have no need to pursue an agnostic approach in terms of inheritance. i do know that postgres inheritance system was discussed a lot in here, but, in my case - where i do want to have a constraint between parent and children - isn't it better to use postgres inheritance instead of duplicating the value to another table? well, i did notice the sqlalchemy example of postgres inheritance https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PostgreSQLInheritance, which uses written ddl and triggers. the problem is that i have extra fields in the inheritance table, which I think it is not a very good approach to postgres inheritance, but, either way, from your experience, what would be your tip? ps: i found this link http://postgresql.nabble.com/Constraint-to-ensure-value-does-NOT-exist-in-another-table-td4493651.html interesting and may be another solution to this, since i already have a table descriptor (the polymorphic_on expression). of course, the approach does require an extra table, but with events I can easily make it work in sqlalchemy. cheers, richard. On 04/14/2015 08:40 AM, Richard Gerd Kuesters | Pollux Automation wrote: here, a better
Re: [sqlalchemy] polymorphic inheritance and unique constraints
well, i'm almost given up ... i'm using concrete now, but it seems that something isn't right. the error: sqlalchemy.exc.ArgumentError: When configuring property 'updated_by' on Mapper|ContainerInstance|pjoin, column 'container_instance.fk_updated_by' is not represented in the mapper's table. Use the `column_property()` function to force this column to be mapped as a read-only attribute. now, what makes me a little hopeless: 1. i have a base object (a simple object), that have some attributes that i want in ALL of my tables (created_at, updated_at, created_by, upated_by), which all of them are @declared_attr; 2. my base object is a declarative_base which uses the object above described as the cls parameter; 3. then, i inherit AbstractConcreteBase and my declarative object to the parent class, having all FKs in it as @declared_attr too; 4. from bla import *, exception. ps: using ConcreteBase, the error is: AttributeError: type object 'ContainerInstance' has no attribute '__mapper__' On 04/15/2015 03:13 PM, Richard Gerd Kuesters wrote: oh, right, concrete! abstract concrete can also do the trick? On 04/15/2015 03:10 PM, Mike Bayer wrote: On 4/15/15 1:59 PM, Richard Gerd Kuesters wrote: oops, i forgot to comment out the fk to the parent table and now it doesn't work: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'container_instance' and 'container_aggregation'. well, it doesn't need it if it's inherited (both db and software level), right? correct, you'd use a concrete setup here from a SQLA perspective. -- 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 mailto:sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- default-signature Atenciosamente, *Richard Gerd Kuesters* *Pollux Automation* Tel.: (47) 3025-9019 rich...@pollux.com.br | www.pollux.com.br http://www.pollux.com.br/ • Linhas de Montagem • Inspeção e Testes • Robótica • Identificação e Rastreabilidade • Software para Manufatura -- 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] polymorphic inheritance and unique constraints
oh, right, concrete! abstract concrete can also do the trick? On 04/15/2015 03:10 PM, Mike Bayer wrote: On 4/15/15 1:59 PM, Richard Gerd Kuesters wrote: oops, i forgot to comment out the fk to the parent table and now it doesn't work: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'container_instance' and 'container_aggregation'. well, it doesn't need it if it's inherited (both db and software level), right? correct, you'd use a concrete setup here from a SQLA perspective. -- 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] Test test/engine/test_pool.py::QueuePoolTest::()::test_queuepool_close fails
Great, thanks Mike, all tests pass for pg8000 without a hitch now. On Wednesday, 8 April 2015 05:12:51 UTC+1, Michael Bayer wrote: On 4/7/15 4:33 PM, Mike Bayer wrote: it's a test that very seldom fails in some cases no matter what, as it's checking reference counts on objects that are expected to be garbage collected without any cycles. A failure of this test is not strictly a bug, it just refers to connections or cursors that are not collected immediately. if it is persistent and only occurs with pg8000 then there might be some cursor cleanup issue going on. 44a9820b4e02f65b3884fa2c016efc has a fix which adds explicit cleanup to a few connection objects that are checked out and not closed, leading to the pool._refs collection to not be empty when that particular test starts. This is backported to 0.9 as well. On 4/7/15 3:22 PM, Tony Locke wrote: Hi, I found the following SQLAlchemy test fails: FAIL test/engine/test_pool.py::QueuePoolTest::()::test_queuepool_close with the stack trace: __ QueuePoolTest.test_queuepool_close ___ Traceback (most recent call last): File /home/tlocke/sqlalchemy/test/engine/test_pool.py, line 805, in test_queuepool_close self._do_testqueuepool(useclose=True) File /home/tlocke/sqlalchemy/test/engine/test_pool.py, line 854, in _do_testqueuepool assert not pool._refs AssertionError: assert not set([sqlalchemy.pool._ConnectionRecord object at 0x7fc03cf1f4a8, sqlalchemy.pool._ConnectionRecord object at 0x7fc03cf1f710, sqlalchemy.pool._ConnectionRecord object at 0x7fc03cf1f5c0]) + where set([sqlalchemy.pool._ConnectionRecord object at 0x7fc03cf1f4a8, sqlalchemy.pool._ConnectionRecord object at 0x7fc03cf1f710, sqlalchemy.pool._ConnectionRecord object at 0x7fc03cf1f5c0]) = pool._refs The versions I'm using are: platform linux -- Python 3.4.0 -- py-1.4.26 -- pytest-2.7.0 I ran it against the tip of the master branch 72329433e78e57c8589e4797df523bb598190b64 and the command I ran was a straightforward: py.test test/engine/test_pool.py::QueuePoolTest I'm sure I'm doing something really obviously wrong, but I'm not sure what, so I'd be grateful if anyone can help. Btw, it was the only test that failed when I ran the entire suite. Thanks, Tony. -- 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+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.com javascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.com javascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] polymorphic inheritance and unique constraints
On 4/15/15 1:59 PM, Richard Gerd Kuesters wrote: oops, i forgot to comment out the fk to the parent table and now it doesn't work: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'container_instance' and 'container_aggregation'. well, it doesn't need it if it's inherited (both db and software level), right? correct, you'd use a concrete setup here from a SQLA perspective. On 04/15/2015 02:55 PM, Richard Gerd Kuesters wrote: the table definitions are listed here: http://pastebin.com/RxS8Lzft i'm using polymorphic associations, but with inheritance (INHERITS) there's no need to do it (imho), so the fk column to the parent table (which is also the pk) can be overriden. using add_is_dependent_on did the trick. i didn't know of such a feature ... thanks for bring it on :) although, is there a way to use it in declarative, intead of: MyModel.__table__.add_is_dependent_on(MyParentModel.__table__) ? cheers, richard. On 04/15/2015 02:44 PM, Mike Bayer wrote: On 4/15/15 1:07 PM, Richard Gerd Kuesters wrote: ok, now i have an issue. i don't know why, but sqlalchemy seems to issue the create table command of inherited postgresql tables before the base one in metadata.create_all(). commenting the inherited table, issuing create all, what do your table defs look like? The tables are created in order of FK dependency, and up until 1.0 there was no ordering beyond that. in 1.0 they will be by table name if there are no FK dependencies. if you're using this with INHERITS types of setups then you should establish which table is dependent on which using add_is_dependent_on(): http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html?highlight=add_is_dependent_on#sqlalchemy.schema.Table.add_is_dependent_on then uncomment the table and issuing create all again seems to work, but ... it's a heck of a workaround (if i think in terms of code). i even tried to use serializable isolation level, but no result. importing models in the desired order doesn't affect the behavior either. well, i'm out of options :) a little help? best regards, richard. On 04/15/2015 11:48 AM, Richard Gerd Kuesters wrote: nevermind. i'm again victim of rtfm :) http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#postgresql-table-options great work on this, btw. it'll simplify my life *A LOT* :) best regards, richard. On 04/15/2015 10:10 AM, Richard Gerd Kuesters wrote: hello Mike! so ... ok, based on this link http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#i-m-getting-a-warning-or-error-about-implicitly-combining-column-x-under-attribute-y (yeah yeah, well, rtfm for me), I was able to make it work like this: code class ContainerInstance(CoreMixin, TimestampMixin): container_instance_id = CoreMixin.column_id() parent_id = CoreMixin.column_fk(container_instance_id, nullable=False) batch_id = CoreMixin.column_fk(Batch.id_, nullable=False) container_instance_type = Column(EnumDictForInt(ContainerInstanceEnum), nullable=False, index=True) __mapper_args__ = { polymorphic_on: container_instance_type, polymorphic_identity: ContainerInstanceEnum.NONE } class ContainerAggregation(ContainerInstance): container_instance_id = CoreMixin.column_fk(ContainerInstance.id_, primary_key=True) container_descriptor_id = CoreMixin.column_fk(ContainerDescriptor.id_, nullable=False) # tada! batch_id = column_property(Column(BigInteger), ContainerInstance.batch_id) __mapper_args__ = { polymorphic_identity: ContainerInstanceEnum.AGGREGATION } UniqueConstraint(ContainerAggregation.container_descriptor_id, ContainerAggregation.batch_id) /code which brings me the question: I'm targeting *only* postgresql, so I have no need to pursue an agnostic approach in terms of inheritance. i do know that postgres inheritance system was discussed a lot in here, but, in my case - where i do want to have a constraint between parent and children - isn't it better to use postgres inheritance instead of duplicating the value to another table? well, i did notice the sqlalchemy example of postgres inheritance https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PostgreSQLInheritance, which uses written ddl and triggers. the problem is that i have extra fields in the inheritance table, which I think it is not a very good approach to postgres inheritance, but, either way, from your experience, what would be your tip? ps: i found this link http://postgresql.nabble.com/Constraint-to-ensure-value-does-NOT-exist-in-another-table-td4493651.html interesting and may be another solution to this, since i already have a table descriptor (the polymorphic_on expression). of course, the approach does require an extra table, but with events I can easily make it work in
Re: [sqlalchemy] polymorphic inheritance and unique constraints
On 4/15/15 1:07 PM, Richard Gerd Kuesters wrote: ok, now i have an issue. i don't know why, but sqlalchemy seems to issue the create table command of inherited postgresql tables before the base one in metadata.create_all(). commenting the inherited table, issuing create all, what do your table defs look like? The tables are created in order of FK dependency, and up until 1.0 there was no ordering beyond that. in 1.0 they will be by table name if there are no FK dependencies. if you're using this with INHERITS types of setups then you should establish which table is dependent on which using add_is_dependent_on(): http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html?highlight=add_is_dependent_on#sqlalchemy.schema.Table.add_is_dependent_on then uncomment the table and issuing create all again seems to work, but ... it's a heck of a workaround (if i think in terms of code). i even tried to use serializable isolation level, but no result. importing models in the desired order doesn't affect the behavior either. well, i'm out of options :) a little help? best regards, richard. On 04/15/2015 11:48 AM, Richard Gerd Kuesters wrote: nevermind. i'm again victim of rtfm :) http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#postgresql-table-options great work on this, btw. it'll simplify my life *A LOT* :) best regards, richard. On 04/15/2015 10:10 AM, Richard Gerd Kuesters wrote: hello Mike! so ... ok, based on this link http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#i-m-getting-a-warning-or-error-about-implicitly-combining-column-x-under-attribute-y (yeah yeah, well, rtfm for me), I was able to make it work like this: code class ContainerInstance(CoreMixin, TimestampMixin): container_instance_id = CoreMixin.column_id() parent_id = CoreMixin.column_fk(container_instance_id, nullable=False) batch_id = CoreMixin.column_fk(Batch.id_, nullable=False) container_instance_type = Column(EnumDictForInt(ContainerInstanceEnum), nullable=False, index=True) __mapper_args__ = { polymorphic_on: container_instance_type, polymorphic_identity: ContainerInstanceEnum.NONE } class ContainerAggregation(ContainerInstance): container_instance_id = CoreMixin.column_fk(ContainerInstance.id_, primary_key=True) container_descriptor_id = CoreMixin.column_fk(ContainerDescriptor.id_, nullable=False) # tada! batch_id = column_property(Column(BigInteger), ContainerInstance.batch_id) __mapper_args__ = { polymorphic_identity: ContainerInstanceEnum.AGGREGATION } UniqueConstraint(ContainerAggregation.container_descriptor_id, ContainerAggregation.batch_id) /code which brings me the question: I'm targeting *only* postgresql, so I have no need to pursue an agnostic approach in terms of inheritance. i do know that postgres inheritance system was discussed a lot in here, but, in my case - where i do want to have a constraint between parent and children - isn't it better to use postgres inheritance instead of duplicating the value to another table? well, i did notice the sqlalchemy example of postgres inheritance https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PostgreSQLInheritance, which uses written ddl and triggers. the problem is that i have extra fields in the inheritance table, which I think it is not a very good approach to postgres inheritance, but, either way, from your experience, what would be your tip? ps: i found this link http://postgresql.nabble.com/Constraint-to-ensure-value-does-NOT-exist-in-another-table-td4493651.html interesting and may be another solution to this, since i already have a table descriptor (the polymorphic_on expression). of course, the approach does require an extra table, but with events I can easily make it work in sqlalchemy. cheers, richard. On 04/14/2015 08:40 AM, Richard Gerd Kuesters | Pollux Automation wrote: here, a better illustration with my actual code: http://pastebin.com/RxS8Lzft best regards, richard. On 04/13/2015 06:30 PM, Mike Bayer wrote: On 4/13/15 4:59 PM, Richard Gerd Kuesters | Pollux Automation wrote: well, this didn't work with upstream 1.0 - sorry, I was in another project and couldn't test it myself. you're not doing the same thing this user was doing in any case... Traceback (most recent call last): File database_test.py, line 46, in module from plx.db.core import * File ../src/plx/db/core.py, line 901, in module UniqueConstraint(ContainerInstance.batch_id, ContainerAggregation.container_descriptor_id,) File /home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py, line 2464, in __init__ ColumnCollectionMixin.__init__(self, *columns, _autoattach=_autoattach) File
Re: [sqlalchemy] polymorphic inheritance and unique constraints
the table definitions are listed here: http://pastebin.com/RxS8Lzft i'm using polymorphic associations, but with inheritance (INHERITS) there's no need to do it (imho), so the fk column to the parent table (which is also the pk) can be overriden. using add_is_dependent_on did the trick. i didn't know of such a feature ... thanks for bring it on :) although, is there a way to use it in declarative, intead of: MyModel.__table__.add_is_dependent_on(MyParentModel.__table__) ? cheers, richard. On 04/15/2015 02:44 PM, Mike Bayer wrote: On 4/15/15 1:07 PM, Richard Gerd Kuesters wrote: ok, now i have an issue. i don't know why, but sqlalchemy seems to issue the create table command of inherited postgresql tables before the base one in metadata.create_all(). commenting the inherited table, issuing create all, what do your table defs look like? The tables are created in order of FK dependency, and up until 1.0 there was no ordering beyond that. in 1.0 they will be by table name if there are no FK dependencies. if you're using this with INHERITS types of setups then you should establish which table is dependent on which using add_is_dependent_on(): http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html?highlight=add_is_dependent_on#sqlalchemy.schema.Table.add_is_dependent_on then uncomment the table and issuing create all again seems to work, but ... it's a heck of a workaround (if i think in terms of code). i even tried to use serializable isolation level, but no result. importing models in the desired order doesn't affect the behavior either. well, i'm out of options :) a little help? best regards, richard. On 04/15/2015 11:48 AM, Richard Gerd Kuesters wrote: nevermind. i'm again victim of rtfm :) http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#postgresql-table-options great work on this, btw. it'll simplify my life *A LOT* :) best regards, richard. On 04/15/2015 10:10 AM, Richard Gerd Kuesters wrote: hello Mike! so ... ok, based on this link http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#i-m-getting-a-warning-or-error-about-implicitly-combining-column-x-under-attribute-y (yeah yeah, well, rtfm for me), I was able to make it work like this: code class ContainerInstance(CoreMixin, TimestampMixin): container_instance_id = CoreMixin.column_id() parent_id = CoreMixin.column_fk(container_instance_id, nullable=False) batch_id = CoreMixin.column_fk(Batch.id_, nullable=False) container_instance_type = Column(EnumDictForInt(ContainerInstanceEnum), nullable=False, index=True) __mapper_args__ = { polymorphic_on: container_instance_type, polymorphic_identity: ContainerInstanceEnum.NONE } class ContainerAggregation(ContainerInstance): container_instance_id = CoreMixin.column_fk(ContainerInstance.id_, primary_key=True) container_descriptor_id = CoreMixin.column_fk(ContainerDescriptor.id_, nullable=False) # tada! batch_id = column_property(Column(BigInteger), ContainerInstance.batch_id) __mapper_args__ = { polymorphic_identity: ContainerInstanceEnum.AGGREGATION } UniqueConstraint(ContainerAggregation.container_descriptor_id, ContainerAggregation.batch_id) /code which brings me the question: I'm targeting *only* postgresql, so I have no need to pursue an agnostic approach in terms of inheritance. i do know that postgres inheritance system was discussed a lot in here, but, in my case - where i do want to have a constraint between parent and children - isn't it better to use postgres inheritance instead of duplicating the value to another table? well, i did notice the sqlalchemy example of postgres inheritance https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/PostgreSQLInheritance, which uses written ddl and triggers. the problem is that i have extra fields in the inheritance table, which I think it is not a very good approach to postgres inheritance, but, either way, from your experience, what would be your tip? ps: i found this link http://postgresql.nabble.com/Constraint-to-ensure-value-does-NOT-exist-in-another-table-td4493651.html interesting and may be another solution to this, since i already have a table descriptor (the polymorphic_on expression). of course, the approach does require an extra table, but with events I can easily make it work in sqlalchemy. cheers, richard. On 04/14/2015 08:40 AM, Richard Gerd Kuesters | Pollux Automation wrote: here, a better illustration with my actual code: http://pastebin.com/RxS8Lzft best regards, richard. On 04/13/2015 06:30 PM, Mike Bayer wrote: On 4/13/15 4:59 PM, Richard Gerd Kuesters | Pollux Automation wrote: well, this didn't work with upstream 1.0 - sorry, I was in another project and couldn't test it myself. you're not doing the same thing this user was
Re: [sqlalchemy] polymorphic inheritance and unique constraints
yup, i know (this pattern) it is not ideal; i was just testing the new features of sa 1.0 regarding postgres (since i'm actually hands-on). i should rewrite a whole part of my model (and listeners and extensions and so on), which are already working with polymorphism. i will change this pattern, but for now i had to know where i can go with those features to reach any other gain in postgres. i listed my steps in 1,2,3,4 so it's easier to spot where my mistake was (because i was sure it was mine). :) when i got the time this kind of implementation deserves, then i'll hook up on rewriting the model. thanks for the support and sorry if this wasted your time! cheers, richard. On 04/15/2015 04:09 PM, Mike Bayer wrote: The pattern you're doing is not what Posgresql INHERITS is really intended for. PG's feature is intended for transparent sharding of data to different tablespaces, not to simulate OR-mapped class hierarchies. The keyword is mis-named in this regard. Concrete inh is in all cases a tough road to travel because it's difficult to relate things to a whole set of tables which each act as the table for a class. -- 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] polymorphic inheritance and unique constraints
On 4/15/15 2:44 PM, Richard Gerd Kuesters wrote: well, i'm almost given up ... i'm using concrete now, but it seems that something isn't right. the error: sqlalchemy.exc.ArgumentError: When configuring property 'updated_by' on Mapper|ContainerInstance|pjoin, column 'container_instance.fk_updated_by' is not represented in the mapper's table. Use the `column_property()` function to force this column to be mapped as a read-only attribute. now, what makes me a little hopeless: 1. i have a base object (a simple object), that have some attributes that i want in ALL of my tables (created_at, updated_at, created_by, upated_by), which all of them are @declared_attr; 2. my base object is a declarative_base which uses the object above described as the cls parameter; 3. then, i inherit AbstractConcreteBase and my declarative object to the parent class, having all FKs in it as @declared_attr too; 4. from bla import *, exception. ps: using ConcreteBase, the error is: AttributeError: type object 'ContainerInstance' has no attribute '__mapper__' The pattern you're doing is not what Posgresql INHERITS is really intended for. PG's feature is intended for transparent sharding of data to different tablespaces, not to simulate OR-mapped class hierarchies. The keyword is mis-named in this regard. Concrete inh is in all cases a tough road to travel because it's difficult to relate things to a whole set of tables which each act as the table for a class. On 04/15/2015 03:13 PM, Richard Gerd Kuesters wrote: oh, right, concrete! abstract concrete can also do the trick? On 04/15/2015 03:10 PM, Mike Bayer wrote: On 4/15/15 1:59 PM, Richard Gerd Kuesters wrote: oops, i forgot to comment out the fk to the parent table and now it doesn't work: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'container_instance' and 'container_aggregation'. well, it doesn't need it if it's inherited (both db and software level), right? correct, you'd use a concrete setup here from a SQLA perspective. -- 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 mailto:sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- default-signature Atenciosamente, *Richard Gerd Kuesters* *Pollux Automation* Tel.: (47) 3025-9019 rich...@pollux.com.br | www.pollux.com.br http://www.pollux.com.br/ • Linhas de Montagem • Inspeção e Testes • Robótica • Identificação e Rastreabilidade • Software para Manufatura -- 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 mailto:sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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: [SQLAlchemy 0.8.2 - NULL value support for version_id_col attribute in mapper()]
Many thanks for your help, Michael! You are so kind to me even with this trivial question. Regards, On Thursday, April 16, 2015 at 9:38:38 AM UTC+7, Khoa Nguyen Minh wrote: Hi everyone, Could you please tell me whether SQLAlchemy 0.8.2 supports NULL value when using version_id_col? Currently, the correspond SQL emitted when updating/deleting is: *where [version_column] = NULL* That always raises StaleDataError exception. Is there a way we can emit custom SQL so that when the original value is NULL then the emitted SQL will be: *where [version_column] is NULL* and in normal case, the emitted SQL will be: *where [version_column] = [value]* Also, I think that if SQLAlchemy provides the custom version_id_generator then why it doesn't support *custom version_id_col where clause when updating/editing*? Really appreciate your helps, Khoa Minh Nguyen -- 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] [SQLAlchemy 0.8.2 - NULL value support for version_id_col attribute in mapper()]
On 4/15/15 10:38 PM, Khoa Nguyen Minh wrote: Hi everyone, Could you please tell me whether SQLAlchemy 0.8.2 supports NULL value when using version_id_col? Currently, the correspond SQL emitted when updating/deleting is: *where [version_column] = NULL* * * That always raises StaleDataError exception. Is there a way we can emit custom SQL so that when the original value is NULL then the emitted SQL will be: *where [version_column] is NULL* NULL can't be supported because the value used for version_id is embedded into a fixed UPDATE statement using a bound parameter. There is no opportunity to produce an alternate UPDATE statement that doesn't use the parameter and encodes IS NULL. The same statement can be passed to the DBAPI with a list of parameter sets. and in normal case, the emitted SQL will be: *where [version_column] = [value]* Also, I think that if SQLAlchemy provides the custom version_id_generator then why it doesn't support *custom version_id_col where clause when updating/editing*? versioning schemes typically only vary in the kinds of values they persist, e.g. integer counters, timestamps, GUIDs, etc. It's not really necessary for a versioning scheme to support NULL; a NULL version doesn't make any sense because it means that a record has an unknown version. Really appreciate your helps, Khoa Minh Nguyen -- 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 mailto:sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- 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] [SQLAlchemy 0.8.2 - NULL value support for version_id_col attribute in mapper()]
Hi everyone, Could you please tell me whether SQLAlchemy 0.8.2 supports NULL value when using version_id_col? Currently, the correspond SQL emitted when updating/deleting is: *where [version_column] = NULL* That always raises StaleDataError exception. Is there a way we can emit custom SQL so that when the original value is NULL then the emitted SQL will be: *where [version_column] is NULL* and in normal case, the emitted SQL will be: *where [version_column] = [value]* Also, I think that if SQLAlchemy provides the custom version_id_generator then why it doesn't support *custom version_id_col where clause when updating/editing*? Really appreciate your helps, Khoa Minh Nguyen -- 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] Not able to filter column by datetime
Hello, I have dozens of queries filtering DateTime columns but I have got one I don't know why it doesn't work. I have: --- q = DBSession.query(func.sum(RecargaCredito.importe), Local.nombre)\ .join((Local, RecargaCredito.locales_id == Local.id)) fl_desde = (datetime.now() - timedelta(days=7)).replace(hour=0, minute=0, second=0) fl_hasta = datetime.now().replace(hour=23, minute=59, second=59) q = q.filter(RecargaCredito.fechayhora = fl_desde).\ filter(RecargaCredito.fechayhora = fl_hasta) q = q.group_by(RecargaCredito.locales_id) f = q.all() --- RecargaCredito looks: --- class RecargaCredito(DeclarativeBase): __tablename__ = 'recargas_credito' __table_args__ = {'mysql_engine':'InnoDB'} id=Column(Integer, primary_key=True) fechayhora = DateTime importe = Column(DECIMAL(9,2)) locales_id=Column(Integer, ForeignKey('locales.id')) local = relation('Local', remote_side = 'Local.id') --- and I get error 'TypeError: can't compare datetime.datetime to VisitableType' I've checked fl_desde and fl_hasta are python datetime objects Do you know what that error says? Regards -- 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.