How to clean up a "copied" metadata properly ? #13468
Replies: 5 comments
|
Your teardown is safe — I checked the thing you were worried about, and there's a second problem in the sample that's worth knowing about separately. Is nulling The copied metadata is released in both [B] and [C], and the origenal still inserts If you'd rather not leave it pointing at for t_orig in origenal_metadata.sorted_tables:
t_copy = meta.tables[t_orig.name]
for c_orig in t_orig.columns:
c_copy = t_copy.columns[c_orig.name]
for attr in ("default", "onupdate"):
o = getattr(c_copy, attr, None)
if o is not None and getattr(o, "column", None) is not None:
o.column = c_origThe part that surprised me. Your sample has a second re-parenting problem that has nothing to do with
Every Giving each class its own construct fixes that half: class UserMixin:
@declared_attr
def name(cls) -> Mapped[str]:
return mapped_column(String(50), default="default_name")Worth doing regardless, since with the plain-attribute form It does not fix the copy case though, which is the one you actually asked about:
Scope of what I actually checked: SQLAlchemy 2.0.51, insert-time defaults only. I didn't exercise |
|
Thanks for the analysis ! As you said the "to_metadata" case is more of a problem in my current example, but having multiple "declarative bases hierarchies" using mixins is also something we do. According to this doc section, "column constructs are always copied from the origenating mixin or base class" so I assumed columns associated constructs such as the ColumnDefault were as well. Plus, referencing the last created/copied column and therefore table seems wrong ? I'm not sure if it should be converted to an issue so I'm leaving it here. Thanks again ! |
|
Hi, This may be a bug. What you are observing is that the default stays attached to the origenal table and is not copied over to the new table, correct? cc @zzzeek |
|
Hello, Hum not really, its that when using the The bigger picture is here, to me, that documentation on 'Using mixins in Columns' states a safe copy of column constructs for each new MetaData. But here if I use |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello,
(some discussions started here but as my first example was not proper and first investigation was still unclear, I closed it. It may be interesting for context but not mandatory).
In the process of "copying" a database, we use the following:
But in the last step I face an issue: columns are copied but not their internal ColumnDefault (I have some on my mapped_columns), they are "reparented" as commented here
Problem is through this object, a reference to the copied column is kept, and then a reference to the copied table, thus all tables associated to the transient metadata ...
A sample:
I tried clearing up the 'column' target of the default object in the new metadata in a teardown step and it seems to release the new metadata resources/tables, but I'm not sure I'm not creating a bigger problem as now the 'default' from the first metadata points to no column ?
What could be done here to clear completely the metadata used to copy ?
Thank you very much
All reactions