MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _add_attribute

Function _add_attribute

lib/sqlalchemy/orm/decl_base.py:2204–2253  ·  view source on GitHub ↗

add an attribute to an existing declarative class. This runs through the logic to determine MapperProperty, adds it to the Mapper, adds a column to the mapped Table, etc.

(
    cls: Type[Any], key: str, value: MapperProperty[Any]
)

Source from the content-addressed store, hash-verified

2202
2203
2204def _add_attribute(
2205 cls: Type[Any], key: str, value: MapperProperty[Any]
2206) -> None:
2207 """add an attribute to an existing declarative class.
2208
2209 This runs through the logic to determine MapperProperty,
2210 adds it to the Mapper, adds a column to the mapped Table, etc.
2211
2212 """
2213
2214 if "__mapper__" in cls.__dict__:
2215 mapped_cls = cast("MappedClassProtocol[Any]", cls)
2216
2217 def _table_or_raise(mc: MappedClassProtocol[Any]) -> Table:
2218 if isinstance(mc.__table__, Table):
2219 return mc.__table__
2220 raise exc.InvalidRequestError(
2221 f"Cannot add a new attribute to mapped class {mc.__name__!r} "
2222 "because it's not mapped against a table."
2223 )
2224
2225 if isinstance(value, Column):
2226 _undefer_column_name(key, value)
2227 _table_or_raise(mapped_cls).append_column(
2228 value, replace_existing=True
2229 )
2230 mapped_cls.__mapper__.add_property(key, value)
2231 elif isinstance(value, _MapsColumns):
2232 mp = value.mapper_property_to_assign
2233 for col, _ in value.columns_to_assign:
2234 _undefer_column_name(key, col)
2235 _table_or_raise(mapped_cls).append_column(
2236 col, replace_existing=True
2237 )
2238 if not mp:
2239 mapped_cls.__mapper__.add_property(key, col)
2240 if mp:
2241 mapped_cls.__mapper__.add_property(key, mp)
2242 elif isinstance(value, MapperProperty):
2243 mapped_cls.__mapper__.add_property(key, value)
2244 elif isinstance(value, QueryableAttribute) and value.key != key:
2245 # detect a QueryableAttribute that's already mapped being
2246 # assigned elsewhere in userland, turn into a synonym()
2247 value = SynonymProperty(value.key)
2248 mapped_cls.__mapper__.add_property(key, value)
2249 else:
2250 type.__setattr__(cls, key, value)
2251 mapped_cls.__mapper__._expire_memoizations()
2252 else:
2253 type.__setattr__(cls, key, value)
2254
2255
2256def _del_attribute(cls: Type[Any], key: str) -> None:

Callers 2

__setattr__Method · 0.85
add_mapped_attributeFunction · 0.85

Calls 8

_undefer_column_nameFunction · 0.85
_table_or_raiseFunction · 0.85
SynonymPropertyClass · 0.85
add_propertyMethod · 0.80
_expire_memoizationsMethod · 0.80
castFunction · 0.50
append_columnMethod · 0.45
__setattr__Method · 0.45

Tested by

no test coverage detected