'Configure an attribute on the mapper representing the
\'polymorphic_on\' column, if applicable, and not
already generated by _configure_properties (which is typical).
Also create a setter function which will assign this
attribute to the value of the \'polymorphic_identity\'
upon instance construction, also if applicable.  This
routine will run when an instance is created.'

def _configure_polymorphic_setter(self, init=False):

    setter = False
    if (self.polymorphic_on is not None):
        setter = True
        if isinstance(self.polymorphic_on, util.string_types):
            try:
                self.polymorphic_on = self._props[self.polymorphic_on]
            except KeyError:
                raise sa_exc.ArgumentError(("Can't    determine    polymorphic_on    value    '%s'    -    no    attribute    is    mapped    to    this    name." % self.polymorphic_on))
        if (self.polymorphic_on in self._columntoproperty):
            prop = self._columntoproperty[self.polymorphic_on]
            polymorphic_key = prop.key
            self.polymorphic_on = prop.columns[0]
            polymorphic_key = prop.key
        elif isinstance(self.polymorphic_on, MapperProperty):
            if (not isinstance(self.polymorphic_on, properties.ColumnProperty)):
                raise sa_exc.ArgumentError('Only    direct    column-mapped    property    or    SQL    expression    can    be    passed    for    polymorphic_on')
            prop = self.polymorphic_on
            self.polymorphic_on = prop.columns[0]
            polymorphic_key = prop.key
        elif (not expression._is_column(self.polymorphic_on)):
            raise sa_exc.ArgumentError('Only    direct    column-mapped    property    or    SQL    expression    can    be    passed    for    polymorphic_on')
        else:
            col = self.mapped_table.corresponding_column(self.polymorphic_on)
            if (col is None):
                setter = False
                instrument = False
                col = self.polymorphic_on
                if (isinstance(col, schema.Column) and ((self.with_polymorphic is None) or (self.with_polymorphic[1].corresponding_column(col) is None))):
                    raise sa_exc.InvalidRequestError(("Could    not    map    polymorphic_on    column    '%s'    to    the    mapped    table    -    polymorphic    loads    will    not    function    properly" % col.description))
            else:
                instrument = True
            key = getattr(col, 'key', None)
            if key:
                if self._should_exclude(col.key, col.key, False, col):
                    raise sa_exc.InvalidRequestError(('Cannot    exclude    or    override    the    discriminator    column    %r' % col.key))
            else:
                self.polymorphic_on = col = col.label('_sa_polymorphic_on')
                key = col.key
            self._configure_property(key, properties.ColumnProperty(col, _instrument=instrument), init=init, setparent=True)
            polymorphic_key = key
    else:
        for mapper in self.iterate_to_root():
            if (mapper.polymorphic_on is not None):
                if (self.mapped_table is mapper.mapped_table):
                    self.polymorphic_on = mapper.polymorphic_on
                else:
                    self.polymorphic_on = self.mapped_table.corresponding_column(mapper.polymorphic_on)
                if (self.polymorphic_on is not None):
                    self._set_polymorphic_identity = mapper._set_polymorphic_identity
                    self._validate_polymorphic_identity = mapper._validate_polymorphic_identity
                else:
                    self._set_polymorphic_identity = None
                return
    if setter:
        def _set_polymorphic_identity(state):
            dict_ = state.dict
            state.get_impl(polymorphic_key).set(state, dict_, state.manager.mapper.polymorphic_identity, None)
        def _validate_polymorphic_identity(mapper, state, dict_):
            if ((polymorphic_key in dict_) and (dict_[polymorphic_key] not in mapper._acceptable_polymorphic_identities)):
                util.warn(('Flushing    object    %s    with    incompatible    polymorphic    identity    %r;    the    object    may    not    refresh    and/or    load    correctly' % (state_str(state), dict_[polymorphic_key])))
        self._set_polymorphic_identity = _set_polymorphic_identity
        self._validate_polymorphic_identity = _validate_polymorphic_identity
    else:
        self._set_polymorphic_identity = None
