Hello Alchemists, See working minimal example below. Here we have Widgets, each associated with a WidgetModel and each having a nominal_size. I wish to add a physical_size property to Widgets, a property which should be a function of both Widget_Model and nominal_size. I'm hung up somewhere in getting this new property mapped in correctly.
# widgetminexample.py from sqlalchemy import * metadata = BoundMetaData('sqlite:///:memory:') widget_table = Table('widget_table', metadata, Column('id', Integer, primary_key=True), Column('nominal_size', Integer), Column('widget_model_id', Integer, ForeignKey('widget_model_table.id'))) widget_model_table = Table('widget_model_table', metadata, Column('id', Integer, primary_key=True), Column('mfr', String(20)), Column('name', String(20))) class Widget(object): def __init__(self, nominal_size=0): self.nominal_size=nominal_size def __repr__(self): return "%s %d" % (self.model, self.nominal_size) class WidgetModel(object): def __init__(self, mfr='mfr', name='name'): self.mfr=mfr self.name=name def __repr__(self): return "%s %s" % (self.mfr, self.name) widget_mapper = mapper(Widget, widget_table, properties = { 'model': relation(WidgetModel, backref='widgets', uselist=False)}) widget_model_mapper = mapper(WidgetModel, widget_model_table) if __name__ == "__main__": metadata.create_all() session=create_session() wm1 = WidgetModel( mfr='FrobozzCo', name='GasketronXL' ) w1 = Widget( nominal_size=10 ) w2 = Widget( nominal_size=8 ) w1.model = wm1 w2.model = wm1 session.save(w1) session.save(w2) session.flush() print 'widgets in database:' for w in session.query(Widget): print w metadata.drop_all() A Widget has the attribute nominal_size. I want each Widget to also have a physical_size. But physical_size needs to be a function of both the nominal_size and the model. Once I've created a table (for each WidgetModel) specifying the relationship of nominal_size to physical_size for that WidgetModel, I don't want to have to know physical_size for each new Widget introduced into the system. Associating a Widget with a WidgetModel needs to result in the Widget getting a physical_size associated with it (looked up via its model and nominal_size). My problem: I'm at a loss to phrase this in SQLAlchemy so far, though it's probably staring me in the face. I considered the following, but I have not been able to come up with a (nominal_size, model)->physical_size mapper that would accomplish the task. widget_size_table = Table( 'widget_size_table', metadata, Column('nominal_size', Integer, primary_key=True), COlumn('widget_model_id', Integer, primary_key=True, ForeignKey('widget_model_table.id')), Column('physical_size', Integer)) Class WidgetSize(object): def __repr__ (self): ... mapper(widget_size_table, WidgetSize, ...) # <here's where I think I'm stuck> Again, I want to make physical_size respond as a property of Widget. I want to be able to initialize a new Widget instance, give it only a nominal_size, associate it with a WidgetModel, and thereby it acquires a physical_size. I'd like to be able to do the following: >>> wm1 = WidgetModel(mfr='FrobozzCo', name='GasketronXL') >>> ws1 = WidgetSize(wm1, 10, 300) >>> w1 = Widget(nominal_size=10) >>> print '%s %s size %d measures: %d millimeters.' % (w1.model.mfr, w1.model.name, w1.nominal_size, w1.physical_size) FrobozzCo GasketronXL size 10 measures: 300 millimeters. My attempts at the proper mapper to do this have, so far, resulted in error messages asking me to specify primary (and perhaps secondary) joins. Should I be joining widget_size_table to widget_model_table and then mapping to that join? Thanks in advance for any advice. Secondary question, regarding session.save(): apparently if I save at least one of my newly instantiated Widgets then my test code will show both as a result of the test query at the end; if I don't save either then neither one will result at the end. Why does saving only one of the instances result in both printing out at the end? Is it because of the widget<>model relation with backref? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---