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

Function synonym

lib/sqlalchemy/orm/_orm_constructors.py:1938–2078  ·  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

1936
1937
1938def synonym(
1939 name: str,
1940 *,
1941 map_column: Optional[bool] = None,
1942 descriptor: Optional[Any] = None,
1943 comparator_factory: Optional[Type[PropComparator[_T]]] = None,
1944 init: Union[_NoArg, bool] = _NoArg.NO_ARG,
1945 repr: Union[_NoArg, bool] = _NoArg.NO_ARG, # noqa: A002
1946 default: Union[_NoArg, _T] = _NoArg.NO_ARG,
1947 default_factory: Union[_NoArg, Callable[[], _T]] = _NoArg.NO_ARG,
1948 compare: Union[_NoArg, bool] = _NoArg.NO_ARG,
1949 kw_only: Union[_NoArg, bool] = _NoArg.NO_ARG,
1950 hash: Union[_NoArg, bool, None] = _NoArg.NO_ARG, # noqa: A002
1951 info: Optional[_InfoType] = None,
1952 doc: Optional[str] = None,
1953 dataclass_metadata: Union[_NoArg, Mapping[Any, Any], None] = _NoArg.NO_ARG,
1954) -> Synonym[Any]:
1955 """Denote an attribute name as a synonym to a mapped property,
1956 in that the attribute will mirror the value and expression behavior
1957 of another attribute.
1958
1959 e.g.::
1960
1961 class MyClass(Base):
1962 __tablename__ = "my_table"
1963
1964 id = Column(Integer, primary_key=True)
1965 job_status = Column(String(50))
1966
1967 status = synonym("job_status")
1968
1969 :param name: the name of the existing mapped property. This
1970 can refer to the string name ORM-mapped attribute
1971 configured on the class, including column-bound attributes
1972 and relationships.
1973
1974 :param descriptor: a Python :term:`descriptor` that will be used
1975 as a getter (and potentially a setter) when this attribute is
1976 accessed at the instance level.
1977
1978 :param map_column: **For classical mappings and mappings against
1979 an existing Table object only**. if ``True``, the :func:`.synonym`
1980 construct will locate the :class:`_schema.Column`
1981 object upon the mapped
1982 table that would normally be associated with the attribute name of
1983 this synonym, and produce a new :class:`.ColumnProperty` that instead
1984 maps this :class:`_schema.Column`
1985 to the alternate name given as the "name"
1986 argument of the synonym; in this way, the usual step of redefining
1987 the mapping of the :class:`_schema.Column`
1988 to be under a different name is
1989 unnecessary. This is usually intended to be used when a
1990 :class:`_schema.Column`
1991 is to be replaced with an attribute that also uses a
1992 descriptor, that is, in conjunction with the
1993 :paramref:`.synonym.descriptor` parameter::
1994
1995 my_table = Table(

Callers 15

AClass · 0.90
BClass · 0.90
AClass · 0.90
AClass · 0.90
UserClass · 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