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)
| 1753 | ) |
| 1754 | |
| 1755 | def _configure_polymorphic_setter(self, init=False): |
| 1756 | """Configure an attribute on the mapper representing the |
| 1757 | 'polymorphic_on' column, if applicable, and not |
| 1758 | already generated by _configure_properties (which is typical). |
| 1759 | |
| 1760 | Also create a setter function which will assign this |
| 1761 | attribute to the value of the 'polymorphic_identity' |
| 1762 | upon instance construction, also if applicable. This |
| 1763 | routine will run when an instance is created. |
| 1764 | |
| 1765 | """ |
| 1766 | setter = False |
| 1767 | polymorphic_key: Optional[str] = None |
| 1768 | |
| 1769 | if self.polymorphic_on is not None: |
| 1770 | setter = True |
| 1771 | |
| 1772 | if isinstance(self.polymorphic_on, str): |
| 1773 | # polymorphic_on specified as a string - link |
| 1774 | # it to mapped ColumnProperty |
| 1775 | try: |
| 1776 | self.polymorphic_on = self._props[self.polymorphic_on] |
| 1777 | except KeyError as err: |
| 1778 | raise sa_exc.ArgumentError( |
| 1779 | "Can't determine polymorphic_on " |
| 1780 | "value '%s' - no attribute is " |
| 1781 | "mapped to this name." % self.polymorphic_on |
| 1782 | ) from err |
| 1783 | |
| 1784 | if self.polymorphic_on in self._columntoproperty: |
| 1785 | # polymorphic_on is a column that is already mapped |
| 1786 | # to a ColumnProperty |
| 1787 | prop = self._columntoproperty[self.polymorphic_on] |
| 1788 | elif isinstance(self.polymorphic_on, MapperProperty): |
| 1789 | # polymorphic_on is directly a MapperProperty, |
| 1790 | # ensure it's a ColumnProperty |
| 1791 | if not isinstance( |
| 1792 | self.polymorphic_on, properties.ColumnProperty |
| 1793 | ): |
| 1794 | raise sa_exc.ArgumentError( |
| 1795 | "Only direct column-mapped " |
| 1796 | "property or SQL expression " |
| 1797 | "can be passed for polymorphic_on" |
| 1798 | ) |
| 1799 | prop = self.polymorphic_on |
| 1800 | else: |
| 1801 | # polymorphic_on is a Column or SQL expression and |
| 1802 | # doesn't appear to be mapped. this means it can be 1. |
| 1803 | # only present in the with_polymorphic selectable or |
| 1804 | # 2. a totally standalone SQL expression which we'd |
| 1805 | # hope is compatible with this mapper's persist_selectable |
| 1806 | col = self.persist_selectable.corresponding_column( |
| 1807 | self.polymorphic_on |
| 1808 | ) |
| 1809 | if col is None: |
| 1810 | # polymorphic_on doesn't derive from any |
| 1811 | # column/expression isn't present in the mapped |
| 1812 | # table. we will make a "hidden" ColumnProperty |
no test coverage detected