On Nov 15, 2012, at 7:33 PM, Rob Crowell wrote:
> Sorry, that got cut off at the end.
>
> class IssueTag(Base):
> __tablename__ = 'issue_user_tag'
>
> sqlalchemy.exc.InvalidRequestError: Table 'issue_user_tag' is already defined
> for this MetaData instance. Specify 'extend_existing=True' to redefine
> options and columns on an existing Table object.
>
> On Thursday, November 15, 2012 7:32:29 PM UTC-5, Rob Crowell wrote:
> I'm working with a denormalized cache schema, and I've run into a situation
> where it would be helpful to be able to create multiple classes that extend
> Base but refer to the same __tablename__. Is this possible to do? I am
> getting this Exception:
> sqlalchemy.exc.InvalidRequestError: Table '[tablename]' is already
> defined for this MetaData instance. Specify 'extend_existing=True' to
> redefine options and columns on an existing Table object.
>
> For a little more insight, we have some attributes that always have exactly
> one value (user who created the issue), and other attributes that can have 1
> or more values (user-defined tags for the issue). If we were being
> exhaustive, we would create two "cached" tables for our issues since
> sometimes we want to display recent issues sometimes by user and sometimes by
> tag:
> * issue_user
> * issue_tag
>
> However, we can get away with writing just one table and querying it with an
> appropriate group_by("user_id") to achieve the same end as having 2 tables.
> Since my application should behave as if there were 2 separate cache tables
> (and I'd like to keep open the option of adding two separate cache tables in
> the future), I would like to have 2 different Base classes representing the
> two ways in which we would query the table. The obvious way of doing this
> doesn't work:
>
> class IssueUser(Base):
> __tablename__ = 'issue_user_tag'
>
> class IssueTag(Base):
>
>
> --
two^H^H^H three ways:
1. map to a Table:
mytable = Table("mytable", Base.metadata, Column(...))
class A(Base):
__table__ = mytable
class B(Base):
__table__ = mytable
1a: variant of 1, map A as you did but use __table__ on B
class A(Base):
__tablename__ = 'mytable'
x = Column(...)
class B(Base):
__table__ = A.__table__
2. use single table inheritance with no discriminator
class MyTable(Base):
__tablename__ = 'mytable'
class A(MyTable):
# ....
class B(MyTable):
# ...
I don't have an understanding of your querying situation yet, discriminating on
group_by() seems a little strange as group_by() is only intended to be used to
group for aggregates, but #1, #1a or #2 should fit the bill.
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sqlalchemy/-/pPc-8bqYaSUJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.