[Q] I want to easily create dynamic sqlalchemy model classes, based on other model classes. [Python] [SQLAlchemy]Edited
I want to easily create dynamic sqlalchemy model classes, based on other model classes. With the new dynamic model classes using a different
__tablename__ and be able to monkey patch relationship and foreign key attributes[TODO]. After some googling I came up with my own
You can see a working gitlab snippet here.
What I want to know is how is this hack gonna bite me in the ass?
The code is not problematic yet. I just googled long and hard to get it working.
I inherit a class to be able to access all its inherited attributes. I change its
__tablename__and set it to
__abstract__to get around how sqlalchemy want to inherit classes. I also allow overriding attributes of the new class.
I then copy all the
__table__columns from my
src_tableto my new
dst_tableand use the sqla
mapper()function to change class attributes to use columns for the new
So there a lot of monkey patching. While in the tests Im running everything seems fine and I maybe want to later copy more then
columnsI still feel I might be missing something that is going to hurt real bad, sooner then later.
So I need input.
EDIT: meh, found a lot of issues with the code and not using it any more. But I did update it and will use it in the future if I want to create dynamic classes for sharding, table_name change etc.