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

Method _configure_pks

lib/sqlalchemy/orm/mapper.py:1593–1718  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1591 return expr
1592
1593 def _configure_pks(self) -> None:
1594 self.tables = sql_util.find_tables(self.persist_selectable)
1595
1596 self._all_tables.update(t for t in self.tables)
1597
1598 self._pks_by_table = {}
1599 self._cols_by_table = {}
1600
1601 all_cols = util.column_set(
1602 chain(*[col.proxy_set for col in self._columntoproperty])
1603 )
1604
1605 pk_cols = util.column_set(c for c in all_cols if c.primary_key)
1606
1607 # identify primary key columns which are also mapped by this mapper.
1608 for fc in set(self.tables).union([self.persist_selectable]):
1609 if fc.primary_key and pk_cols.issuperset(fc.primary_key):
1610 # ordering is important since it determines the ordering of
1611 # mapper.primary_key (and therefore query.get())
1612 self._pks_by_table[fc] = util.ordered_column_set( # type: ignore # noqa: E501
1613 fc.primary_key
1614 ).intersection(
1615 pk_cols
1616 )
1617 self._cols_by_table[fc] = util.ordered_column_set(fc.c).intersection( # type: ignore # noqa: E501
1618 all_cols
1619 )
1620
1621 if self._primary_key_argument:
1622 coerced_pk_arg = [
1623 (
1624 self._str_arg_to_mapped_col("primary_key", c)
1625 if isinstance(c, str)
1626 else c
1627 )
1628 for c in (
1629 coercions.expect(
1630 roles.DDLConstraintColumnRole,
1631 coerce_pk,
1632 argname="primary_key",
1633 )
1634 for coerce_pk in self._primary_key_argument
1635 )
1636 ]
1637 else:
1638 coerced_pk_arg = None
1639
1640 # if explicit PK argument sent, add those columns to the
1641 # primary key mappings
1642 if coerced_pk_arg:
1643 for k in coerced_pk_arg:
1644 if k.table not in self._pks_by_table:
1645 self._pks_by_table[k.table] = util.OrderedSet()
1646 self._pks_by_table[k.table].add(k)
1647
1648 # otherwise, see that we got a full PK for the mapped table
1649 elif (
1650 self.persist_selectable not in self._pks_by_table

Callers 1

__init__Method · 0.95

Calls 10

_logMethod · 0.95
reduce_columnsMethod · 0.80
updateMethod · 0.45
unionMethod · 0.45
issupersetMethod · 0.45
intersectionMethod · 0.45
addMethod · 0.45
warnMethod · 0.45
corresponding_columnMethod · 0.45

Tested by

no test coverage detected