Fetch Addresses from the database

Persisted Address instances

Reahl provides glue so you can persist Addresses in a database using SqlAlchemy:

  • Use the SqlAlchemy Session and Base provided by sqlalchemysupport.
  • __tablename__ identifies the table Address instances go into;
  • The Columns map similarly named attributes to database columns;
  • The id Column is the primary key for Addresses;
  • Persist an Address instance with Session.add().
from reahl.sqlalchemysupport import Session, Base
from sqlalchemy import Column, Integer, UnicodeText
class Address(Base):
    __tablename__ = 'addressbook1_address'

    id            = Column(Integer, primary_key=True)
    email_address = Column(UnicodeText)
    name          = Column(UnicodeText)

    def save(self):
        Session.add(self)

Change AddressBookPanel to query the database

class AddressBookPanel(Div):
    def __init__(self, view):
        super(AddressBookPanel, self).__init__(view)

        self.add_child(H(view, 1, text='Addresses'))

        for address in Session.query(Address).all():
            self.add_child(AddressBox(view, address))

Register Address with .reahlproject

Reahl needs to know about persisted classes so it can (for example):

  • create database tables
  • migrate older databases to newer schemas

Register Address in your project by adding it to the <persisted> section of the .reahlproject file:

<persisted>
    <class locator="reahl.doc.examples.tutorial.addressbook1.addressbook1:Address"/>
</persisted>

Housekeeping and your database schema

Whenever you change a .reahlproject file though, be sure to run:

reahl setup -- develop -N

To create a database, run:

reahl createdbuser etc  # If you have not already
reahl createdb etc
reahl createdbtables etc

More useful commands:

reahl dropdb etc
reahl dropdbtables etc

Configuration

Wonder where the database is? Check applicable config settings with:

reahl listconfig etc -i

To see the actual current values of those:

reahl listconfig etc -v