@validates is not called on inherited subclasses (Joined Table Inheritance)

Issue #3626 closed
Diego Pego created an issue

I think this is related to but do not duplicates #2943

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    data = Column(String)
    reg_type = Column(String)

    @validates('data')
    def validate(self, key, value):
        return "yup A " + value

    __mapper_args__ = {
        'polymorphic_identity':'a',
        'polymorphic_on':reg_type
    }


class B(A):
    __tablename__ = 'b'
    other_data = Column(String)

    @validates('other_data ')
    def validate_other_data(self, key, value):
        # >>>>> this is never called
        assert value.strip() != ''
        return value

    __mapper_args__ = {
        'polymorphic_identity':'TYPE_B',
    }

Comments (4)

  1. Mike Bayer repo owner

    works fine, your test has a typo in it (remove the space after 'other data '):

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class A(Base):
        __tablename__ = 'a'
    
        id = Column(Integer, primary_key=True)
        data = Column(String)
        reg_type = Column(String)
    
        @validates('data')
        def validate(self, key, value):
            return "yup A " + value
    
        __mapper_args__ = {
            'polymorphic_identity':'a',
            'polymorphic_on':reg_type
        }
    
    
    class B(A):
        __tablename__ = 'b'
    
        id = Column(ForeignKey('a.id'), primary_key=True)
        other_data = Column(String)
    
        @validates('other_data')
        def validate_other_data(self, key, value):
            # >>>>> this is never called
            assert value.strip() != ''
            return value
    
        __mapper_args__ = {
            'polymorphic_identity':'TYPE_B',
        }
    
    b1 = B()
    b1.other_data = ''
    

    output:

    #!
    
    
     File "test.py", line 33, in validate_other_data
        assert value.strip() != ''
    AssertionError
    

    please confirm no issue thanks!

  2. Diego Pego reporter

    Really sorry. I confirm no issue! I was really tired. Thank you for the great work at sqlalchemy!

    Sorry for the typo, but this was not the problem too. The problem is that I was expecting the @validate to execute on a non-changed field. I want a string field not to be blank.

  3. Log in to comment