1.0.0b3: regression with single-table inheritance and constraint added in a subclass

Issue #3341 resolved
Gabor Gombas created an issue

Hi,

The attached example works fine with 0.9.7, but 1.0.0b3 gives the following error: sqlalchemy.exc.ArgumentError: Can't add unnamed column to column collection

Gabor

Comments (10)

  1. Mike Bayer repo owner

    this approach is a hack in the first place, as 0.9 doesn't support table_args on the inherited class and this relies upon CheckConstraint happening to do a certain kind of autoattach (e.g. UniqueConstraint doesn't do it).

    that said, the autoattach feature in 1.0 is more aggressive but to work around unnamed columns might require a lot of rearrangement.

  2. Mike Bayer repo owner

    In 0.9, CheckConstraint has the omission of even being able to track columns, so that's why this doesn't raise in 0.9.

  3. Gabor Gombas reporter

    In the real code I use __declare_last__() to process __extra_table_args__, but the error triggered even without that, so I omitted it to make the test case simpler.

  4. Gabor Gombas reporter

    So what's the solution? Do I have to start passing explicit names to Column() to be able to use CheckConstraint?

  5. Mike Bayer repo owner

    well the immediate works-right-now solution is:

    class VlanInterface(Interface):
        parent_id = Column(ForeignKey(Interface.id, name='iface_parent_fk'))
    
        __mapper_args__ = {'polymorphic_identity': 'vlan'}
    
    CheckConstraint(or_(
        VlanInterface.parent_id != null(),
        Interface.interface_type != 'vlan'),
        name='iface_vlan_ck')
    

    the very interesting way that your test case actually manages to do "auto-attach" is because one column is attached to the table already with its name, on the superclass, so it finds the table.

  6. Mike Bayer repo owner

    it will work in 0.9 and 1.0. I'm poking around still but your test is really a much thinner edge case than it initially seems (only CheckConstraint, only on single-table inheritance subclass w/ extra column. wont work any other way in 0.9)

  7. Mike Bayer repo owner
    • changed milestone to 1.0
    • changed component to orm

    I might "wontfix" this but let me look at it later as it isn't as urgent. I'm sure other people are going to hit similar issues.

  8. Mike Bayer repo owner
    • The "auto-attach" feature of constraints such as :class:.UniqueConstraint and :class:.CheckConstraint has been further enhanced such that when the constraint is associated with non-table-bound :class:.Column objects, the constraint will set up event listeners with the columns themselves such that the constraint auto attaches at the same time the columns are associated with the table. This in particular helps in some edge cases in declarative but is also of general use. fixes #3341

    → <<cset bdcaa0f6ca58>>

  9. Log in to comment