Associate a SQL type with this mutable Python type. This establishes listeners that will detect ORM mappings against the given type, adding mutation event trackers to those mappings. The type is returned, unconditionally as an instance, so that :meth:`.as_mutable` c
(cls, sqltype: _TypeEngineArgument[_T])
| 656 | |
| 657 | @classmethod |
| 658 | def as_mutable(cls, sqltype: _TypeEngineArgument[_T]) -> TypeEngine[_T]: |
| 659 | class="st">"""Associate a SQL type with this mutable Python type. |
| 660 | |
| 661 | This establishes listeners that will detect ORM mappings against |
| 662 | the given type, adding mutation event trackers to those mappings. |
| 663 | |
| 664 | The type is returned, unconditionally as an instance, so that |
| 665 | :meth:`.as_mutable` can be used inline:: |
| 666 | |
| 667 | Table( |
| 668 | class="st">"mytable", |
| 669 | metadata, |
| 670 | Column(class="st">"id", Integer, primary_key=True), |
| 671 | Column(class="st">"data", MyMutableType.as_mutable(PickleType)), |
| 672 | ) |
| 673 | |
| 674 | Note that the returned type is always an instance, even if a class |
| 675 | is given, and that only columns which are declared specifically with |
| 676 | that type instance receive additional instrumentation. |
| 677 | |
| 678 | To associate a particular mutable type with all occurrences of a |
| 679 | particular type, use the :meth:`.Mutable.associate_with` classmethod |
| 680 | of the particular :class:`.Mutable` subclass to establish a global |
| 681 | association. |
| 682 | |
| 683 | .. warning:: |
| 684 | |
| 685 | The listeners established by this method are *global* |
| 686 | to all mappers, and are *not* garbage collected. Only use |
| 687 | :meth:`.as_mutable` for types that are permanent to an application, |
| 688 | not with ad-hoc types else this will cause unbounded growth |
| 689 | in memory usage. |
| 690 | |
| 691 | class="st">""" |
| 692 | sqltype = types.to_instance(sqltype) |
| 693 | |
| 694 | class="cm"># a SchemaType will be copied when the Column is copied, |
| 695 | class="cm"># and we'll lose our ability to link that type back to the original. |
| 696 | class="cm"># so track our original type w/ columns |
| 697 | if isinstance(sqltype, SchemaEventTarget): |
| 698 | |
| 699 | @event.listens_for(sqltype, class="st">"before_parent_attach") |
| 700 | def _add_column_memo( |
| 701 | sqltyp: TypeEngine[Any], |
| 702 | parent: Column[_T], |
| 703 | ) -> None: |
| 704 | parent.info[class="st">"_ext_mutable_orig_type"] = sqltyp |
| 705 | |
| 706 | schema_event_check = True |
| 707 | else: |
| 708 | schema_event_check = False |
| 709 | |
| 710 | def listen_for_type( |
| 711 | mapper: Mapper[_T], |
| 712 | class_: Union[DeclarativeAttributeIntercept, type], |
| 713 | ) -> None: |
| 714 | _APPLIED_KEY = class="st">"_ext_mutable_listener_applied" |
| 715 |