Finding original "type" when using as_mutable()

12 views
Skip to first unread message

Lele Gaifax

unread,
Jul 10, 2019, 3:20:40 AM7/10/19
to sqlal...@googlegroups.com
Hi,

I would like to find an automated way to collect all columns of any mapped
class which type is declared as

ColType.as_mutable(ConcreteType)

In an application I'm developing I use sqlalchemy-media to handle multi-media
"attachments" to avoid storing the actual media within the database.

As an example, consider
http://sqlalchemy-media.dobisel.com/tutorials/image.html#defining-the-model,
where the Person class contains an "image" column defined approximately as

class Person(Base):
...
image = Column(Image.as_mutable(Json))

Inspecting the class, I find that the "type" of the image column is actually
"Json", so that alone does not help. I tried different strategies, but I could
not find a way to determine it is related to "Image".

Basically, in pseudo code, I'd like to distill an helper function like

def find_image_columns(base_class):
for cls in base_class.__subclasses__():
m = class_mapper(cls)
for col in m.columns:
if <col is related to Image>: # this is my glass of water
yield cls, col

I followed the as_mutable() code path, and found that it installs various
"hooks" to achieve its goal, but could not figure out how I could "inspect"
those to find out the link with the Image class.

As a last resort, I can easily go around all my models and put some kind of a
marker in the "info" slot of the relevant columns, and then check for that,
but I wonder if there is an less intrusive way... knowing and loving SA I bet
there is and it's just a matter of pronouncing the right incantation :-)

Thank you in advance for any hint,
ciao, lele.
--
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it | -- Fortunato Depero, 1929.

Mike Bayer

unread,
Jul 10, 2019, 10:54:57 AM7/10/19
to noreply-spamdigest via sqlalchemy
yeah there is not a "nice" way to do that now without some new feature being added to support that.   If "Image" is your own type, I would include that "as_mutable" sets up additional state somewhere, column.info is one place.   sorry there is not a better solution.


As a last resort, I can easily go around all my models and put some kind of a
marker in the "info" slot of the relevant columns, and then check for that,
but I wonder if there is an less intrusive way... knowing and loving SA I bet
there is and it's just a matter of pronouncing the right incantation :-)

Thank you in advance for any hint,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
le...@metapensiero.it  |                 -- Fortunato Depero, 1929.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper


To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Lele Gaifax

unread,
Jul 11, 2019, 1:38:49 AM7/11/19
to sqlal...@googlegroups.com
"Mike Bayer" <mik...@zzzcomputing.com> writes:

> yeah there is not a "nice" way to do that now without some new feature being added to support
> that. If "Image" is your own type, I would include that "as_mutable" sets up additional state
> somewhere, column.info is one place. sorry there is not a better solution.

Thank you Mike,
Reply all
Reply to author
Forward
0 new messages