[EMAIL PROTECTED] wrote: > Firstly when retrieving information using mappers, I have not been very > successful at all in this If someone could offer a 2 line example of > this problem. > > Retrieving Email.address if User.name == 'jack' > > ... > > I have the same problem with locating a row and replacing one value in > that row. So in the above example if I wanted to change the > Email.address value for user.name == jack .
Since I'm also trying to learn SA, I wrote a commented example code that guides you through all necessary steps from creating the tables and the mappers to adding users and addresses. Most of it is already contained in the tutorial. # import everything you need: from sqlalchemy import * # connect to your database: db = create_engine(...) metadata = BoundMetaData(db) # define the tables: user_table = Table('user', metadata, Column('id', Integer, primary_key=True), Column('name', String(40)), Column('password', String(10)), Column('age', Integer)) email_table = Table('mail_address', metadata, Column('id', Integer, primary_key=True), Column('address', String(100), nullable=False), Column('user_id', Integer, ForeignKey('user.id'))) # create the tables: email_table.drop() user_table.drop() user_table.create() email_table.create() # define your Mapper objects: class User(object): def __init__(self, name, age, password): self.name = name self.age = age self.password = password def __str__(self): return self.name class Email(object): def __init__(self, address, user_id=None): self.address = address self.user_id = user_id def __str__(self): return self.address # create the mappings: user_mapper = mapper(User, user_table) email_mapper = mapper(Email, email_table) user_mapper.add_property('addresses', relation(Email)) # obtain a session: session = create_session() # now you can start to play: # create users: user1 = User('Stephen', 42, 'Joshua') user2 = User('Willy', 21, 'free') user3 = User('Jack', 33, 'forgot') # save the users: session.save(user1) session.save(user2) session.save(user3) # create email addresses: user1.addresses.append(Email('[EMAIL PROTECTED]', user1.id)) user1.addresses.append(Email('[EMAIL PROTECTED]', user1.id)) user2.addresses.append(Email('[EMAIL PROTECTED]', user2.id)) user3.addresses.append(Email('[EMAIL PROTECTED]')) user3.addresses.append(Email('[EMAIL PROTECTED]')) # save everything to the database: session.flush() # read user Jack anew from the database: del user3 # forget about Jack user = session.query(User).get_by(name='Jack') # print all email addresses of Jack: print print user, 'is', user.age, 'years old' print 'and has these email addresses:' for adr in user.addresses: print '\t', adr # Jack gets one year older: user.age += 1 # delete Jacl's aol email addresses: user.addresses = [adr for adr in user.addresses if not adr.address.endswith('@aol.com')] # Jack has got a new email address: user.addresses.append(Email('[EMAIL PROTECTED]')) session.flush() # store changes # read user Jack anew from the database: del user user = session.query(User).get_by(name='Jack') # again, print all email addresses of Jack: print print user, 'is now', user.age, 'years old' print 'and has these email addresses:' for adr in user.addresses: print '\t', adr --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---