class MyParent(Base):____________
foo_id = Column(Integer, Sequence('foo_id_seq'), primary_key=True)
foo_name = Column(Unicode(64), nullable=False)
foo_type = Column(Integer, nullable=False)
__mapper_args__ = {
"polymorphic_on": foo_type,
"polymorphic_identity": 0
}
class MyChild1(MyParent):
foo_id = Column(Integer, ForeignKey(MyParent.foo_id), primary_key=True)
bar_id = Column(Integer, ForeignKey(AnotherEntity.bar_id), nullable=False)
child1_specific_name = Column(Unicode(5), nullable=False)
child1_baz_stuff = Column(Boolean, default=False)
__mapper_args__ = {
"polymorphic_identity": 1
}
__table_args__ = (
UniqueConstraint(bar_id, child1_specific_name,), # works, bar_id is in MyChild1
)
class MyChild2(MyParent):
foo_id = Column(Integer, ForeignKey(MyParent.foo_id), primary_key=True)
bar_id = Column(Integer, ForeignKey(AnotherEntity.bar_id), nullable=False)
child2_specific_code = Column(UUID, nullable=False)
child2_baz_stuff = Column(Float, nullable=False)
__mapper_args__ = {
"polymorphic_identity": 2
}
__table_args__ = (
UniqueConstraint(bar_id, child2_specific_code,), # works, bar_id is in MyChild2
)
class MyParent(Base):____________
foo_id = Column(Integer, Sequence('foo_id_seq'), primary_key=True)
foo_name = Column(Unicode(64), nullable=False)
foo_type = Column(Integer, nullable=False)
bar_id = Column(Integer, ForeignKey(AnotherEntity.bar_id), nullable=False) # since both child uses bar_id, why not having it on the parent?
__mapper_args__ = {
"polymorphic_on": foo_type,
"polymorphic_identity": 0
}
class MyChild1(MyParent):
foo_id = Column(Integer, ForeignKey(MyParent.foo_id), primary_key=True)
child1_specific_name = Column(Unicode(5), nullable=False)
child1_baz_stuff = Column(Boolean, default=False)
__mapper_args__ = {
"polymorphic_identity": 1
}
__table_args__ = (
UniqueConstraint(MyParent.bar_id, child1_specific_name,), # will it work?
)
class MyChild2(MyParent):
foo_id = Column(Integer, ForeignKey(MyParent.foo_id), primary_key=True)
child2_specific_code = Column(UUID, nullable=False)
child2_baz_stuff = Column(Float, nullable=False)
__mapper_args__ = {
"polymorphic_identity": 2
}
__table_args__ = (
UniqueConstraint(MyParent.bar_id, child2_specific_code,), # will it work?
)
well, this didn't work with upstream 1.0 - sorry, I was in another project and couldn't test it myself.
<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>
[...]
2015-04-15 14:26:32,859 INFO sqlalchemy.engine.base.Engine {'name': u'user_setting'}
2015-04-15 14:26:32,860 INFO sqlalchemy.engine.base.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%(name)s
2015-04-15 14:26:32,860 INFO sqlalchemy.engine.base.Engine {'name': u'adapter'}
2015-04-15 14:26:32,860 INFO sqlalchemy.engine.base.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%(name)s
2015-04-15 14:26:32,860 INFO sqlalchemy.engine.base.Engine {'name': u'permission_override_history'}
2015-04-15 14:26:32,864 INFO sqlalchemy.engine.base.Engine
CREATE TABLE system_unit_setting_history (
dt_created_on TIMESTAMP WITH TIME ZONE NOT NULL,
dt_updated_on TIMESTAMP WITH TIME ZONE NOT NULL,
b_active BOOLEAN NOT NULL,
b_deleted BOOLEAN NOT NULL,
pk_system_unit_setting_id BIGSERIAL NOT NULL,
fk_system_unit_id BIGINT NOT NULL,
e_key SMALLINT NOT NULL,
u_value VARCHAR(255) NOT NULL,
fk_updated_by BIGINT,
fk_created_by BIGINT,
pk_version INTEGER NOT NULL,
PRIMARY KEY (pk_system_unit_setting_id, pk_version)
)
2015-04-15 14:26:32,864 INFO sqlalchemy.engine.base.Engine {}
2015-04-15 14:26:32,877 INFO sqlalchemy.engine.base.Engine COMMIT
2015-04-15 14:26:32,882 INFO sqlalchemy.engine.base.Engine
CREATE TABLE organization_address_history (
dt_created_on TIMESTAMP WITH TIME ZONE NOT NULL,
dt_updated_on TIMESTAMP WITH TIME ZONE NOT NULL,
b_active BOOLEAN NOT NULL,
b_deleted BOOLEAN NOT NULL,
pk_organization_address_id BIGSERIAL NOT NULL,
fk_organization_id BIGINT NOT NULL,
fk_address_id BIGINT NOT NULL,
fk_updated_by BIGINT,
fk_created_by BIGINT,
pk_version INTEGER NOT NULL,
PRIMARY KEY (pk_organization_address_id, pk_version)
)
2015-04-15 14:26:32,882 INFO sqlalchemy.engine.base.Engine {}
2015-04-15 14:26:32,894 INFO sqlalchemy.engine.base.Engine COMMIT
2015-04-15 14:26:32,903 INFO sqlalchemy.engine.base.Engine
CREATE TABLE container_aggregation (
pk_fk_container_instance_id BIGINT NOT NULL,
fk_container_descriptor_id BIGINT NOT NULL,
u_container_ean VARCHAR(20) NOT NULL,
b_generated_container_ean BOOLEAN NOT NULL,
fk_adapter_id BIGINT NOT NULL,
e_container_aggregation_status SMALLINT NOT NULL,
PRIMARY KEY (pk_fk_container_instance_id)
)
INHERITS ( container_instance )
2015-04-15 14:26:32,903 INFO sqlalchemy.engine.base.Engine {}
2015-04-15 14:26:32,904 INFO sqlalchemy.engine.base.Engine ROLLBACK
Traceback (most recent call last):
File "database_test.py", line 425, in <module>
run()
File "database_test.py", line 114, in run
create_all()
File "database_test.py", line 95, in create_all
get_base('CORE_VPAK').metadata.create_all()
File "/home/richard/.pyenv/versions/vpak-pollux-2.7.9/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3614, in create_all
tables=tables)
[...]
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,
![]() |
![]() |
| • Linhas de Montagem • Inspeção e Testes • Robótica |
• Identificação e
Rastreabilidade • Software para Manufatura |
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?
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:
--
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.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
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__'"