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

Method _set_parent

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

Source from the content-addressed store, hash-verified

2312 )
2313
2314 def _set_parent( # type: ignore[override]
2315 self,
2316 parent: SchemaEventTarget,
2317 *,
2318 all_names: Dict[str, Column[Any]],
2319 allow_replacements: bool,
2320 **kw: Any,
2321 ) -> None:
2322 table = parent
2323 assert isinstance(table, Table)
2324 if not self.name:
2325 raise exc.ArgumentError(
2326 "Column must be constructed with a non-blank name or "
2327 "assign a non-blank .name before adding to a Table."
2328 )
2329
2330 self._reset_memoizations()
2331
2332 if self.key is None:
2333 self.key = self.name
2334
2335 existing = getattr(self, "table", None)
2336 if existing is not None and existing is not table:
2337 raise exc.ArgumentError(
2338 f"Column object '{self.key}' already "
2339 f"assigned to Table '{existing.description}'"
2340 )
2341
2342 extra_remove = None
2343 existing_col = None
2344 conflicts_on = ""
2345
2346 if self.key in table._columns:
2347 existing_col = table._columns[self.key]
2348 if self.key == self.name:
2349 conflicts_on = "name"
2350 else:
2351 conflicts_on = "key"
2352 elif self.name in all_names:
2353 existing_col = all_names[self.name]
2354 extra_remove = {existing_col}
2355 conflicts_on = "name"
2356
2357 if existing_col is not None:
2358 if existing_col is not self:
2359 if not allow_replacements:
2360 raise exc.DuplicateColumnError(
2361 f"A column with {conflicts_on} " f"""'{
2362 self.key if conflicts_on == 'key' else self.name
2363 }' """ f"is already present in table '{table.name}'."
2364 )
2365 for fk in existing_col.foreign_keys:
2366 table.foreign_keys.remove(fk)
2367 if fk.constraint in table.constraints:
2368 # this might have been removed
2369 # already, if it's a composite constraint
2370 # and more than one col being replaced
2371 table.constraints.remove(fk.constraint)

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