MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / replace

Method replace

lib/sqlalchemy/sql/base.py:2433–2521  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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)

Callers 8

addMethod · 0.95
test_replaceMethod · 0.95
test_replace_no_matchMethod · 0.95

Calls 12

_append_new_columnMethod · 0.95
_ColumnMetricsClass · 0.85
maxClass · 0.85
addMethod · 0.45
appendMethod · 0.45
difference_updateMethod · 0.45
getMethod · 0.45
disposeMethod · 0.45
insertMethod · 0.45
_deannotateMethod · 0.45
clearMethod · 0.45
updateMethod · 0.45