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

Method as_mutable

lib/sqlalchemy/ext/mutable.py:660–743  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

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

Callers 15

misc_ext.pyFile · 0.80
ThingClass · 0.80
AClass · 0.80
define_tablesMethod · 0.80
define_tablesMethod · 0.80
define_tablesMethod · 0.80
define_tablesMethod · 0.80
define_tablesMethod · 0.80
AbstractFooClass · 0.80
MixinClass · 0.80
define_tablesMethod · 0.80

Calls 1

listenMethod · 0.45

Tested by 8

define_tablesMethod · 0.64
define_tablesMethod · 0.64
define_tablesMethod · 0.64
define_tablesMethod · 0.64
define_tablesMethod · 0.64
define_tablesMethod · 0.64
define_tablesMethod · 0.64