Sqlalchemy: Cancel MetaData.remove (table)

For some rather complex unit test environment, we want to dynamically modify the tables contained in the metadata. Dropping tables from it is supported with .remove(table)

or even .clear()

. But how can such a table be added later?

There is a method in MetaData _add_table(name, schema)

, but that doesn't seem to be the official way. Also, it Table._set_parent(metadata)

seems more appropriate if you want to use the route of using internal methods.

There is also one Table.tometadata(metadata)

that creates a new instance of the table that is bound to the new metadata. So I could create a complete new metadata and attach all the "needed" tables. But that would mean that all the rest of the code needs to know about the new table instances associated with the new metadata. I don't want to go this route.

UPDATE: We are now looking at fork / multiprocessing to load tables only in a subprocess (sandboxed environment) so that only that subprocess is "tainted" and the following tests do not suffer. I note this here for completeness, it is not strictly related to the main question, but may help others who find this question.

+3


source to share


1 answer


mutating an object MetaData

is almost not supported in a non-additive way, and in general you shouldn't build use cases on top of it. Using new objects MetaData

that contain the schema you are looking for in your particular scenario will work best.



+1


source







All Articles