Thanks for the advice, and sorry I didn't provide the correct information, but ctx.current.flush() makes no difference. r.cancelled_user is (nearly: 19/20) always None immediately after the assign and flush with no database access being made. Interestingly, the user_id/user relationship is always correct
I will just have to stick to using the hack on the Receipt object to get the cancelled_user until I am more familiar with SA to work out what is going on. Ali Missing Tables: suppliers_table = Table('supplier', meta, Column('id', Integer, autoincrement=True, primary_key=True), Column('name', String), Column('address', String), Column('postcode', String), Column('telephone', String), ) class Supplier(NamedObject): """Supplier""" def get_long_string(self): return '%s (%s %s %s)' % (self.name, self.address, self.postcode, self.telephone) assign_mapper(ctx, Supplier, suppliers_table) forms_table = Table('form', meta, Column('id', Integer, autoincrement=True, primary_key=True), Column('name', String), ) class Form(NamedObject): """Drug Form""" assign_mapper(ctx, Form, forms_table) drugs_table = Table('drug', meta, Column('id', Integer, autoincrement=True, primary_key=True), Column('name', String), Column('strength', String), Column('stock', Integer), Column('form_id', Integer, ForeignKey('form.id')) ) class Drug(object): """Drug Definition""" def __str__(self): return '%s (%s %s)' % (self.name, self.form, self.strength) str = property(__str__) assign_mapper(ctx, Drug, drugs_table, properties = dict( form = relation(Form, backref='drugs') ) ) On Feb 20, 9:04 pm, Michael Bayer <[EMAIL PROTECTED]> wrote: > well I cant run the program since its not complete (no Drug, Supplier > class/tables defined, etc), but if id hazard a guess id say you > shouldnt be calling "flush()" on your class instances, since it will > not flush any changes on any dependencies. call ctx.current.flush() > instead. > > On Feb 20, 2007, at 1:40 PM, Ali wrote: > > > > > Ok, > > > from sqlalchemy import * > > > from sqlalchemy.ext.assignmapper import assign_mapper > > from sqlalchemy.ext.sessioncontext import SessionContext > > > meta = DynamicMetaData() > > > ctx = SessionContext(create_session) > > > def make_engine(uri=None): > > if uri is None: > > uri = 'sqlite:///:memory:' > > engine = create_engine(uri, echo=True) > > return engine > > > def connect(uri=None): > > meta.connect(make_engine(uri=uri)) > > > users_table = Table('user', meta, > > Column('id', Integer, autoincrement=True, primary_key=True), > > Column('name', String), > > Column('password', String), > > Column('registration', String), > > ) > > > class User(object): > > """User""" > > def get_is_admin(self): > > return self.id == 1 > > is_admin = property(get_is_admin) > > > def __str__(self): > > s = self.name > > if self.registration: > > s = '%s (%s)' % (s, self.registration) > > return s > > > assign_mapper(ctx, User, users_table) > > > stockreceipt_table = Table('stockreceipt', meta, > > Column('id', Integer, autoincrement=True, primary_key=True), > > Column('drug_id', Integer, ForeignKey('drug.id')), > > Column('date', Date), > > Column('quantity', Integer), > > Column('supplier_id', Integer, ForeignKey('supplier.id')), > > Column('user_id', Integer, ForeignKey('user.id')), > > Column('cancelled', Boolean), > > Column('cancelled_reason', String), > > Column('cancelled_user_id', Integer, ForeignKey('user.id')), > > Column('internal_transfer', Boolean), > > Column('invoice_number', String), > > Column('internal_from', String), > > ) > > > class Receipt(object): > > """Stock Receipt""" > > def get_cancelled_user_hack(self): > > return User.get_by(id=self.cancelled_user_id) > > cancelled_user_hack = property(get_cancelled_user_hack) > > > assign_mapper(ctx, Receipt, stockreceipt_table, > > properties=dict( > > user=relation(User, backref='receipts', > > primaryjoin=stockreceipt_table.c.user_id == > > users_table.c.id), > > cancelled_user=relation(User, backref='cancelled_receipts', > > lazy=False, > > primaryjoin=stockreceipt_table.c.cancelled_user_id == > > users_table.c.id), > > supplier=relation(Supplier, backref='receipts'), > > drug=relation(Drug, backref='receipts'), > > ) > > ) > > > meta.create_all() > > connect() > > >>>> u = User(name='Ali') > >>>> u.flush() > >>>> r = Receipt(user_id=u.id) > >>>> r.flush() > >>>> r.user is u > > True > >>>> r.cancelled_user_id = u.id > >>>> r.flush() > >>>> r.cancelled_user is u > > True > > > Now, as I mentioned, r.cancelled_user is set correctly in this > > example, but not (always) in my application, although it is used > > similarly. (I am happy to paste 2000 lines of PyGTK if you like). When > > the attribute is accessed, there is no SQL output at all, although in > > this example there is. > > > Thanks for any assistance, > > > Ali > > > On Feb 20, 6:04 pm, "Michael Bayer" <[EMAIL PROTECTED]> wrote: > >> why dont you attach a script that runs completely ? use a > >> "sqlite://" > >> database. > > >> On Feb 20, 12:14 pm, "Ali" <[EMAIL PROTECTED]> wrote: > > >>> By "both", I mean user and receipt tables > > >>> On 20 Feb, 17:12, "Ali" <[EMAIL PROTECTED]> wrote: > > >>>> I left out the fields for brevity of example. They both have an id > >>>> column defined as primary keys. > > >>>> On 20 Feb, 17:11, "Michael Bayer" <[EMAIL PROTECTED]> wrote: > > >>>>> On Feb 20, 12:01 pm, "Ali" <[EMAIL PROTECTED]> wrote: > > >>>>>> Here is my code (with other fields removed): > > >>>>>> stockreceipt_table = Table('stockreceipt', meta, > >>>>>> Column('user_id', Integer, ForeignKey('user.id')), > >>>>>> # Cancellation > >>>>>> Column('cancelled_user_id', Integer, ForeignKey('user.id')), > >>>>>> ) > > >>>>>> class Receipt(object): > >>>>>> """Stock Receipt""" > > >>>>>> assign_mapper(ctx, Receipt, stockreceipt_table, > >>>>>> properties=dict( > >>>>>> user=relation(User, backref='receipts', > >>>>>> primaryjoin=stockreceipt_table.c.user_id == > >>>>>> users_table.c.id), > >>>>>> cancelled_user=relation(User, > >>>>>> backref='cancelled_receipts', > >>>>>> lazy=False, > >>>>>> primaryjoin=stockreceipt_table.c.cancelled_user_id == > >>>>>> users_table.c.id), > >>>>>> ) > > >>>>> that cant be right, since theres no primary key expressed > >>>>> either in > >>>>> the stockreceipt_table or in your Receipt mapper. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---