MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _set_parent

Method _set_parent

lib/sqlalchemy/sql/schema.py:2588–2710  ·  view source on GitHub ↗
(  # type: ignore[override]
        self,
        parent: SchemaEventTarget,
        *,
        all_names: Dict[str, Column[Any]],
        allow_replacements: bool,
        index: Optional[int] = None,
        **kw: Any,
    )

Source from the content-addressed store, hash-verified

2586 )
2587
2588 def _set_parent( # type: ignore[override]
2589 self,
2590 parent: SchemaEventTarget,
2591 *,
2592 all_names: Dict[str, Column[Any]],
2593 allow_replacements: bool,
2594 index: Optional[int] = None,
2595 **kw: Any,
2596 ) -> None:
2597 table = parent
2598 assert isinstance(table, Table)
2599 if not self.name:
2600 raise exc.ArgumentError(
2601 "Column must be constructed with a non-blank name or "
2602 "assign a non-blank .name before adding to a Table."
2603 )
2604
2605 self._reset_memoizations()
2606
2607 if self.key is None:
2608 self.key = self.name
2609
2610 existing = getattr(self, "table", None)
2611 if existing is not None and existing is not table:
2612 raise exc.ArgumentError(
2613 f"Column object '{self.key}' already "
2614 f"assigned to Table '{existing.description}'"
2615 )
2616
2617 extra_remove = None
2618 existing_col = None
2619 conflicts_on = ""
2620
2621 if self.key in table._columns:
2622 existing_col = table._columns[self.key]
2623 if self.key == self.name:
2624 conflicts_on = "name"
2625 else:
2626 conflicts_on = "key"
2627 elif self.name in all_names:
2628 existing_col = all_names[self.name]
2629 extra_remove = {existing_col}
2630 conflicts_on = "name"
2631
2632 if existing_col is not None:
2633 if existing_col is not self:
2634 if not allow_replacements:
2635 raise exc.DuplicateColumnError(
2636 f"A column with {conflicts_on} " f"""'{
2637 self.key if conflicts_on == 'key' else self.name
2638 }' """ f"is already present in table '{table.name}'."
2639 )
2640 for fk in existing_col.foreign_keys:
2641 table.foreign_keys.remove(fk)
2642 if fk.constraint in table.constraints:
2643 # this might have been removed
2644 # already, if it's a composite constraint
2645 # and more than one col being replaced

Callers

nothing calls this directly

Calls 10

IndexClass · 0.85
UniqueConstraintClass · 0.85
_reset_memoizationsMethod · 0.80
_replaceMethod · 0.80
append_constraintMethod · 0.80
_set_remote_tableMethod · 0.80
removeMethod · 0.45
warnMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected