MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _configure_polymorphic_setter

Method _configure_polymorphic_setter

lib/sqlalchemy/orm/mapper.py:1836–2032  ·  view source on GitHub ↗

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_identi

(self, init=False)

Source from the content-addressed store, hash-verified

1834 )
1835
1836 def _configure_polymorphic_setter(self, init=False):
1837 """Configure an attribute on the mapper representing the
1838 'polymorphic_on' column, if applicable, and not
1839 already generated by _configure_properties (which is typical).
1840
1841 Also create a setter function which will assign this
1842 attribute to the value of the 'polymorphic_identity'
1843 upon instance construction, also if applicable. This
1844 routine will run when an instance is created.
1845
1846 """
1847 setter = False
1848 polymorphic_key: Optional[str] = None
1849
1850 if self.polymorphic_on is not None:
1851 setter = True
1852
1853 if isinstance(self.polymorphic_on, str):
1854 # polymorphic_on specified as a string - link
1855 # it to mapped ColumnProperty
1856 try:
1857 self.polymorphic_on = self._props[self.polymorphic_on]
1858 except KeyError as err:
1859 raise sa_exc.ArgumentError(
1860 "Can't determine polymorphic_on "
1861 "value '%s' - no attribute is "
1862 "mapped to this name." % self.polymorphic_on
1863 ) from err
1864
1865 if self.polymorphic_on in self._columntoproperty:
1866 # polymorphic_on is a column that is already mapped
1867 # to a ColumnProperty
1868 prop = self._columntoproperty[self.polymorphic_on]
1869 elif isinstance(self.polymorphic_on, MapperProperty):
1870 # polymorphic_on is directly a MapperProperty,
1871 # ensure it's a ColumnProperty
1872 if not isinstance(
1873 self.polymorphic_on, properties.ColumnProperty
1874 ):
1875 raise sa_exc.ArgumentError(
1876 "Only direct column-mapped "
1877 "property or SQL expression "
1878 "can be passed for polymorphic_on"
1879 )
1880 prop = self.polymorphic_on
1881 else:
1882 # polymorphic_on is a Column or SQL expression and
1883 # doesn't appear to be mapped. this means it can be 1.
1884 # only present in the with_polymorphic selectable or
1885 # 2. a totally standalone SQL expression which we'd
1886 # hope is compatible with this mapper's persist_selectable
1887 col = self.persist_selectable.corresponding_column(
1888 self.polymorphic_on
1889 )
1890 if col is None:
1891 # polymorphic_on doesn't derive from any
1892 # column/expression isn't present in the mapped
1893 # table. we will make a "hidden" ColumnProperty

Callers 2

__init__Method · 0.95
_set_polymorphic_onMethod · 0.95

Calls 5

_should_excludeMethod · 0.95
_configure_propertyMethod · 0.95
iterate_to_rootMethod · 0.95
corresponding_columnMethod · 0.45
labelMethod · 0.45

Tested by

no test coverage detected