On 12/15/2015 07:46 AM, Lele Gaifax wrote:
> Hi all,
>
> I'd like to investigate a possible "special" usage/abuse of comments on schema
> objects, available for example under PostgreSQL.
>
> In many applications, I use the "info" slot of table's columns to store visual
> settings, mainly a "label" and an "hint", something like
>
> class Player(GloballyUnique, Base):
> """A single person."""
>
> __tablename__ = 'players'
> "Related table."
>
> ## Columns
>
> idplayer = Column(
> intid_t, Sequence('gen_idplayer', optional=True),
> primary_key=True,
> nullable=False,
> info=dict(label=N_('Player ID'),
> hint=N_('Unique ID of the player.')))
> """Primary key."""
>
> firstname = Column(
> name_t,
> nullable=False,
> info=dict(label=N_('First name'),
> hint=N_('First name of the player.')))
> """Player's first name."""
>
> Those strings are then picked up (and possibly translated) by a library[*]
> that exposes a SA selectable as a webservice, producing either a "meta
> description" of its resultset (that a client app can use to configure a
> grid/table/form to display/edit it), or effectively executing the query given
> required parameters.
>
> This morning I was asked "would it be possible to use PG `col_description()`
> for that, storing the info slot as a JSON string in the database itself,
> opening the way of using SA reflection capability instead of an explicit
> declaration?". Uhm, what he said!
>
> I spent some time inside SA code, to understand if/where I could hook an
> extension to the reflection mechanism that would allow me to fetch such extra
> information for each column. It seems that the `Inspector.get_columns()`
> declares an "attrs" slot, carrying "optional column attributes", but no
> dialect uses it AFAICT.
>
> I could of course tweak/monkey patch the `PGDialect.get_columns()`, but I
> wonder if there is a better way hidden somewhere (... it wouldn't be a great
> surprise! :-).
>
> What would you suggest?
The column_reflect event supports this - you can populate the info
dictionary right there from information that you get out of the
col_description system view.
http://docs.sqlalchemy.org/en/rel_1_0/core/events.html?highlight=column_reflect#sqlalchemy.events.DDLEvents.column_reflect
"column_info" is a dictionary, so you'd say:
@event.listens_for(Table, 'column_reflect')
def receive_column_reflect(inspector, table, column_info):
column_info['info'] = {"my_comment": "a comment"}
>
> Thanks for any hint,
> ciao, lele.
>
> [*] https://pypi.python.org/pypi/metapensiero.sqlalchemy.proxy
>
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.