Allow dynamic column names in CompositeProperty

25 views
Skip to first unread message

Samer Atiani

unread,
Jan 19, 2017, 1:53:58 PM1/19/17
to sqlalchemy
I would like to allow some of the CompositeProperty columns I'm building to get their names dynamically, in a manner that mirrors how columns get their names in sqlalchemy's declarative mapper. The best way I found to implement this behavior is to do the following:

class DeferredCompositeProperty(CompositeProperty):
    def get_column_names(self, key):
        raise NotImplementedError

    def undefer_column_names(self, key):
        for col, name in zip(self.columns, self.get_column_names(key)):
            col.name = name
            col.key = name

class CustomDeclarativeMeta(DeclarativeMeta):
    def __new__(cls, name, bases, d):
        def undefer(k, v):
            if isinstance(v, DeferredCompositeProperty):
                v.undefer_column_names(k)

        for k, v in d.iteritems():
            undefer(k, v)

        for base in bases:
            for k, v in base.__dict__.iteritems():
                undefer(k, v)

        return DeclarativeMeta.__new__(cls, name, bases, d)

class MoneyComposite(DeferredCompositeProperty):
def __init__(self):
DeferredCompositeProperty.__init__(self, <SomeCompositeClass>, Column(Integer), Column(String))
def get_column_names(self, key):
return [
'{}_amount'.format(key),
‘{}_currency’.format(key)
]
Base = declarative_base(metaclass=CustomDeclarativeMeta)

class ExampleModel(Base):
funds = MoneyComposite()


This would result in ExampleModel having the columns: funds_amount and funds_currency automatically without having to specify them directly.

Am I doing this right or is there a better way?

mike bayer

unread,
Jan 19, 2017, 4:23:01 PM1/19/17
to sqlal...@googlegroups.com
not sure offhand - you really don't just want to say:

funds = MoneyComposite('funds')

? sure would be simpler.



>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Samer Atiani

unread,
Jan 19, 2017, 8:44:31 PM1/19/17
to sqlal...@googlegroups.com
Definitely simpler :)

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.

To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/snArVxEm4mA/unsubscribe.

To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.

To post to this group, send email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages