I'm working on learning SQLAlchemy to use for my business tracking
application. I have the views written (in tkinter) and two versions of
model.py as the model. Long ago I asked about using base or declarative base
but no longer have that thread saved.

I've read about base mapping and declarative base and am still confused.

The short version, which I believe is correct, references the existing,
populated postgresql-12 tables:

"""
  This is the SQLAlchemy declarative mapping python classes to postgres
  tables for the business tracker.
"""

from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Unicode, Integer, String, Date
from sqlalchemy.orm import sessionmaker
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import CheckConstraint
from sqlalchemy.orm import Session
from sqlalchemy.dialects import postgresql

"""Base = declarative_base()"""
Base = automap_base()

engine = create_engine('postgresql+psycopg2:///bustrac')

# reflect the tables
Base.prepare(engine, reflect=True)

state_code = postgresql.Enum('AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 
'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 
'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 
'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 
'WA', 'WV', 'WI', 'WY', 'AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON', 
'PE', 'QC', 'SK', 'YT', name='state_code')

Industries = Base.classes.industries
Status = Base.classes.status
StatusTypes = Base.classes.statusTypes
ActivityTypes = Base.classes.activityTypes
Organizations = Base.classes.organizations
Locations = Base.classes.locations
People = Base.classes.people
Activities = Base.classes.activities
Cases = Base.classes.cases
Projects = Base.classes.projects

Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)

The longer version defines the classes for each table.

When I fully understand the model format I'll ask for where to start in the
docs to learn how to structure the controller portion using .sql scripts
already written.

TIA,

Rich

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/alpine.LNX.2.20.2012181231590.29974%40salmo.appl-ecosys.com.

Reply via email to