MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / synonym

Function synonym

lib/sqlalchemy/orm/_orm_constructors.py:1973–2113  ·  view source on GitHub ↗

Denote an attribute name as a synonym to a mapped property, in that the attribute will mirror the value and expression behavior of another attribute. e.g.:: class MyClass(Base): __tablename__ = "my_table" id = Column(Integer, primary_key=True)

(
    name: str,
    *,
    map_column: Optional[bool] = None,
    descriptor: Optional[Any] = None,
    comparator_factory: Optional[Type[PropComparator[_T]]] = None,
    init: Union[_NoArg, bool] = _NoArg.NO_ARG,
    repr: Union[_NoArg, bool] = _NoArg.NO_ARG,  # noqa: A002
    default: Union[_NoArg, _T] = _NoArg.NO_ARG,
    default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG,
    compare: Union[_NoArg, bool] = _NoArg.NO_ARG,
    kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG,
    hash: Union[_NoArg, bool, None] = _NoArg.NO_ARG,  # noqa: A002
    info: Optional[_InfoType] = None,
    doc: Optional[str] = None,
    dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None] = _NoArg.NO_ARG,
)

Source from the content-addressed store, hash-verified

1971
1972
1973def synonym(
1974 name: str,
1975 *,
1976 map_column: Optional[bool] = None,
1977 descriptor: Optional[Any] = None,
1978 comparator_factory: Optional[Type[PropComparator[_T]]] = None,
1979 init: Union[_NoArg, bool] = _NoArg.NO_ARG,
1980 repr: Union[_NoArg, bool] = _NoArg.NO_ARG, # noqa: A002
1981 default: Union[_NoArg, _T] = _NoArg.NO_ARG,
1982 default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG,
1983 compare: Union[_NoArg, bool] = _NoArg.NO_ARG,
1984 kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG,
1985 hash: Union[_NoArg, bool, None] = _NoArg.NO_ARG, # noqa: A002
1986 info: Optional[_InfoType] = None,
1987 doc: Optional[str] = None,
1988 dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None] = _NoArg.NO_ARG,
1989) -> Synonym[Any]:
1990 """Denote an attribute name as a synonym to a mapped property,
1991 in that the attribute will mirror the value and expression behavior
1992 of another attribute.
1993
1994 e.g.::
1995
1996 class MyClass(Base):
1997 __tablename__ = "my_table"
1998
1999 id = Column(Integer, primary_key=True)
2000 job_status = Column(String(50))
2001
2002 status = synonym("job_status")
2003
2004 :param name: the name of the existing mapped property. This
2005 can refer to the string name ORM-mapped attribute
2006 configured on the class, including column-bound attributes
2007 and relationships.
2008
2009 :param descriptor: a Python :term:`descriptor` that will be used
2010 as a getter (and potentially a setter) when this attribute is
2011 accessed at the instance level.
2012
2013 :param map_column: **For classical mappings and mappings against
2014 an existing Table object only**. if ``True``, the :func:`.synonym`
2015 construct will locate the :class:`_schema.Column`
2016 object upon the mapped
2017 table that would normally be associated with the attribute name of
2018 this synonym, and produce a new :class:`.ColumnProperty` that instead
2019 maps this :class:`_schema.Column`
2020 to the alternate name given as the "name"
2021 argument of the synonym; in this way, the usual step of redefining
2022 the mapping of the :class:`_schema.Column`
2023 to be under a different name is
2024 unnecessary. This is usually intended to be used when a
2025 :class:`_schema.Column`
2026 is to be replaced with an attribute that also uses a
2027 descriptor, that is, in conjunction with the
2028 :paramref:`.synonym.descriptor` parameter::
2029
2030 my_table = Table(

Callers 15

AClass · 0.90
AClass · 0.90
BClass · 0.90
AClass · 0.90
AClass · 0.90
setup_mappersMethod · 0.90
test_synonymMethod · 0.90
MyUserClass · 0.90
setup_mappersMethod · 0.90
User2Class · 0.90
Address2Class · 0.90
test_synonym_optionsMethod · 0.90

Calls 2

SynonymClass · 0.85
_AttributeOptionsClass · 0.85

Tested by 15

setup_mappersMethod · 0.72
test_synonymMethod · 0.72
setup_mappersMethod · 0.72
test_synonym_optionsMethod · 0.72
test_synonym_fkMethod · 0.72
test_column_prefixMethod · 0.72
test_add_propertyMethod · 0.72