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,
)
| 2130 | del self._index[len(self._collection)] |
| 2131 | |
| 2132 | def replace( |
| 2133 | self, |
| 2134 | column: _NAMEDCOL, |
| 2135 | extra_remove: Optional[Iterable[_NAMEDCOL]] = None, |
| 2136 | ) -> None: |
| 2137 | """add the given column to this collection, removing unaliased |
| 2138 | versions of this column as well as existing columns with the |
| 2139 | same key. |
| 2140 | |
| 2141 | e.g.:: |
| 2142 | |
| 2143 | t = Table("sometable", metadata, Column("col1", Integer)) |
| 2144 | t.columns.replace(Column("col1", Integer, key="columnone")) |
| 2145 | |
| 2146 | will remove the original 'col1' from the collection, and add |
| 2147 | the new column under the name 'columnname'. |
| 2148 | |
| 2149 | Used by schema.Column to override columns during table reflection. |
| 2150 | |
| 2151 | """ |
| 2152 | |
| 2153 | if extra_remove: |
| 2154 | remove_col = set(extra_remove) |
| 2155 | else: |
| 2156 | remove_col = set() |
| 2157 | # remove up to two columns based on matches of name as well as key |
| 2158 | if column.name in self._index and column.key != column.name: |
| 2159 | other = self._index[column.name][1] |
| 2160 | if other.name == other.key: |
| 2161 | remove_col.add(other) |
| 2162 | |
| 2163 | if column.key in self._index: |
| 2164 | remove_col.add(self._index[column.key][1]) |
| 2165 | |
| 2166 | if not remove_col: |
| 2167 | self._append_new_column(column.key, column) |
| 2168 | return |
| 2169 | new_cols: List[Tuple[str, _NAMEDCOL, _ColumnMetrics[_NAMEDCOL]]] = [] |
| 2170 | replaced = False |
| 2171 | for k, col, metrics in self._collection: |
| 2172 | if col in remove_col: |
| 2173 | if not replaced: |
| 2174 | replaced = True |
| 2175 | new_cols.append( |
| 2176 | (column.key, column, _ColumnMetrics(self, column)) |
| 2177 | ) |
| 2178 | else: |
| 2179 | new_cols.append((k, col, metrics)) |
| 2180 | |
| 2181 | if remove_col: |
| 2182 | self._colset.difference_update(remove_col) |
| 2183 | |
| 2184 | for rc in remove_col: |
| 2185 | for metrics in self._proxy_index.get(rc, ()): |
| 2186 | metrics.dispose(self) |
| 2187 | |
| 2188 | if not replaced: |
| 2189 | new_cols.append((column.key, column, _ColumnMetrics(self, column))) |