Table properties

The name of the data table generated in the RDBMS database will correspond to the __tablename__ property of a model class. The primary key for each table is used for relationships and queries and must be defined using the keyword primary_key. The Column class and the String, Float and Integer type classes are called from SQLAlchemy and are used to define table columns to be generated within the underlying RDBMS (thus allowing the programmer to avoid crafting CREATE TABLE statements for each variety of SQL used by the major RDBMS).

For example, the Arena class will be used to manage a table that has four columns—a String name field, two Float fields (longitude and latitude), and a POINT geometry type with an SRID or EPSG spatial reference system ID of 4326, corresponding to the WGS 1984 coordinate system (http://spatialreference.org/ref/epsg/wgs-84/):

    # Define the Arena class, which will model the Arena database table
class Arena(Base):
__tablename__ = 'arena'
id = Column(Integer, primary_key=True)
name = Column(String)
longitude = Column(Float)
latitude = Column(Float)
geom = Column(Geometry(geometry_type='POINT', srid=4326))

Like the Arena class, the following classes use a String name column. For the geometry type, they also use SRID 4326, but they use the MULTIPOLYGON geometry type to store the complex multipolygon geometries used to model these geographies. For tables with relationships, as in the case of the County, District, and State classes, there are also special classes used to manage table relationships and queries between tables.

These special classes include the ForeignKey class and the relationship function. The ForeignKey class is passed an id parameter and passed to a Column class, associating the child row's with the parent. The relationship function allows two-way queries. The backref keyword generates a function that instantiates an instance of the joined table's model:

    # Define the County class
class County(Base):
__tablename__ = 'county'
id = Column(Integer, primary_key=True)
name = Column(String)
state_id = Column(Integer, ForeignKey('state.id'))
state_ref = relationship("State",backref='county')
geom = Column(Geometry(geometry_type='MULTIPOLYGON',srid=4326))

# Define the District class
class District(Base):
__tablename__ = 'district'
id = Column(Integer, primary_key=True)
district = Column(String)
name = Column(String)
state_id = Column(Integer, ForeignKey('state.id'))
state_ref = relationship("State",backref='district')
geom = Column(Geometry(geometry_type='MULTIPOLYGON',srid=4326))


The County class and the District class will have a relationship with the State class, allowing session queries that call the State class. This relationship makes it easy to find which US state a county or congressional district is located in. The state_id column builds the relationship, and the state_ref field references the parent State class. For the State class, the counties and districts have their own backref references, allowing the parent State class to access the associated counties/districts:

    # Define the State class
class State(Base):
__tablename__ = 'state'
id = Column(Integer, primary_key=True)
name = Column(String)
statefips = Column(String)
stpostal = Column(String)
counties = relationship('County', backref='state')
districts = relationship('District', backref='state')
geom =
Column(Geometry(geometry_type='MULTIPOLYGON',srid=4326))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset