> Am 26.11.2011 15:26 schrieb "Mark Erbaugh" <[email protected]>:
>
> I'm using a ColumnMixin to have a subset of columns common in two tables. Is
> there an easy way to populate the common columns in one descendent table with
> the corresponding columns in a row of the other descendent tables, and can
> this be a method of the ColumnMixin class? Ideally, I'd like the copy method
> to dynamically respond to changes in the ColumnMixin class (i.e. if I add a
> column defiinition to the mixin, I don't want to have to modify the copy
> method).
>
> Thanks,
> Mark
On Nov 27, 2011, at 4:06 AM, Robert Forkel wrote:
> Hi,
> I'm doing something similar and ended up giving all columns contributed by a
> mixin a common prefix, and have the copy method loop over all columns of an
> object, picking out the right ones.
> Regards
> Robert
>
Robert,
Thanks for the reply. I ended up with a different approach, illustrated by the
code snippet:
_sizing_pump_columns = (
('pump', String(6)),
('mod_date', String(19)),
('curve', String(15)),
('eq_gpm', Float),
('eq_psi', Float),
)
current_pump_table = Table('current_pump', Base.metadata,
Column('id', Integer, primary_key=True),
Column('user', String, ForeignKey('user._user')),
*[Column(*i) for i in _sizing_pump_columns]
)
pump_table = Table('pump', Base.metadata,
Column('id', Integer, primary_key=True),
Column('sizing_id', Integer, ForeignKey('sizing.id')),
*[Column(*i) for i in _sizing_pump_columns]
)
class _SizedPumpBase(object):
def copy(self, other):
"""
copy data from other into self
"""
for i in _sizing_pump_columns:
self.__dict__[i[0]] = other.__dict[i[0]]
class SizedPump(_SizedPumpBase):
pass
class SizedPumpCurrent(_SizedPumpBase):
pass
mapper(SizedPumpCurrent, current_pump_table)
mapper(SizedPump, pump_table)
I switched from SA declarative to the separate table and mapper, so I could use
the _sizing_pump_columns tuple for the definition of the current_pump_table and
pump_table objects and use field names in the copy method. As of now, it's
passing my unit tests, but I'd appreciate any comments specifically on
something I might have missed.
Mark
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.