add the given column to this collection, removing unaliased versions of this column as well as existing columns with the same key. e.g.:: t = Table("sometable", metadata, Column("col1", Integer)) t.columns.replace(Column("col1", Integer, key="column
(
self,
column: _NAMEDCOL,
*,
extra_remove: Optional[Iterable[_NAMEDCOL]] = None,
index: Optional[int] = None,
)
| 2431 | del self._index[len(self._collection)] |
| 2432 | |
| 2433 | def replace( |
| 2434 | self, |
| 2435 | column: _NAMEDCOL, |
| 2436 | *, |
| 2437 | extra_remove: Optional[Iterable[_NAMEDCOL]] = None, |
| 2438 | index: Optional[int] = None, |
| 2439 | ) -> None: |
| 2440 | """add the given column to this collection, removing unaliased |
| 2441 | versions of this column as well as existing columns with the |
| 2442 | same key. |
| 2443 | |
| 2444 | e.g.:: |
| 2445 | |
| 2446 | t = Table("sometable", metadata, Column("col1", Integer)) |
| 2447 | t.columns.replace(Column("col1", Integer, key="columnone")) |
| 2448 | |
| 2449 | will remove the original 'col1' from the collection, and add |
| 2450 | the new column under the name 'columnname'. |
| 2451 | |
| 2452 | Used by schema.Column to override columns during table reflection. |
| 2453 | |
| 2454 | """ |
| 2455 | |
| 2456 | if extra_remove: |
| 2457 | remove_col = set(extra_remove) |
| 2458 | else: |
| 2459 | remove_col = set() |
| 2460 | # remove up to two columns based on matches of name as well as key |
| 2461 | if column.name in self._index and column.key != column.name: |
| 2462 | other = self._index[column.name][1] |
| 2463 | if other.name == other.key: |
| 2464 | remove_col.add(other) |
| 2465 | |
| 2466 | if column.key in self._index: |
| 2467 | remove_col.add(self._index[column.key][1]) |
| 2468 | |
| 2469 | if not remove_col: |
| 2470 | self._append_new_column(column.key, column, index=index) |
| 2471 | return |
| 2472 | new_cols: List[Tuple[str, _NAMEDCOL, _ColumnMetrics[_NAMEDCOL]]] = [] |
| 2473 | replace_index = None |
| 2474 | |
| 2475 | for idx, (k, col, metrics) in enumerate(self._collection): |
| 2476 | if col in remove_col: |
| 2477 | if replace_index is None: |
| 2478 | replace_index = idx |
| 2479 | new_cols.append( |
| 2480 | (column.key, column, _ColumnMetrics(self, column)) |
| 2481 | ) |
| 2482 | else: |
| 2483 | new_cols.append((k, col, metrics)) |
| 2484 | |
| 2485 | if remove_col: |
| 2486 | self._colset.difference_update(remove_col) |
| 2487 | |
| 2488 | for rc in remove_col: |
| 2489 | for metrics in self._proxy_index.get(rc, ()): |
| 2490 | metrics.dispose(self) |