The normal PythonOnWheels workflow is pretty simple and straight forward. You create Models, define schemas, apply them to a Database and everything is fine. But sometimes you do not start from zero. Sometimes there already is a Database and you just want to create a model, an API or a whole Application around it. This is also perfectly possible with PythonOnWheels. We will cover how this works in the following short hands-on tutorial.
Prerequisites:
pip install PyMySQLmysql -u root -p < employees.sqlI added a user named employee_db and granted the according rights. But for tests you can also go with root. Just as usual configure your sql DB connection in config.py:
"type" : "mysql+pymysql",
"dbname" : "employees", # just a name for non file based DBs
"host" : "127.0.0.1",
"port" : 3306,
"user" : "employee_db",
"passwd" : "<your_pwd_here>",
We will go through the workflow once for the employees table
Just generate a model as usual:
python generate_model.py -n employee -t sqlNow we need to change four things:
@relation.setup_sql_schema()
class Employee(Base, metaclass=PowBaseMeta):
#
# cerberus style schema
#
schema = {}
# define a custom tablename to link for this model:
__tablename__ = "employees"
# Toggle using the pow schema extensions (id, created_at, last_updated)
_use_pow_schema_attrs = False
# define class attributes/variables here that should be included in to_dict()
# conversion and also handed to the encoders but that are NOT part of the schema.
include_attributes=[]
# Add sqlalchemy table_args here. Add "autoload" : True for reflection
__table_args__ = { "extend_existing": True, "autoload" : True }
#
# init
#
def __init__(self, **kwargs):
self.setup_instance_values()
self.init_on_load(**kwargs)
python init_sqldb_environment.py
----------------------------------------------------------------------
updated migration environment: mysql+pymysql://employee_db:employee@127.0.0.1:3306/employee
---------------------------------------------------------------------- >>> from testapp.models.sql.employee import Employee
setup_schema:employee
>>> e=Employee()
trying to find possible observer in testapp.models.sql.employee_observer.EmployeeObserver
>>>
emp_no : None (primary key)
birth_date : None
first_name : None
last_name : None
gender : None
hire_date : None
>>> e.find_first()
emp_no : 10001 (primary key)
birth_date : 1953-09-02
first_name : Georgi
last_name : Facello
gender : M
hire_date : 1986-06-26
>>>
>>> e.find(Employee.first_name.like("Ge%")).count()3740Let's first count. This is what Heidi SQL says:
This is what PoW counts:
>>> e.find_all().count()
300024
>>> e=Employee()>>> e.first_name="python"
>>> e.last_name="on wheels"
>>> import datetime
>>> e.birth_date=datetime.date.today()
>>> e.hire_date=datetime.date.today()
>>> e.gender="F"
>>> e.emp_no="999997"
>>> e.upsert()
>>> e.find_first(Employee.emp_no=="999997")
emp_no : 999997 (primary key)
birth_date : 2019-04-08
first_name : python
last_name : on wheels
gender : F
hire_date : 2019-04-08
Sum it up:
We connected to an existing Database and reflected an existing schema. All we needed to do was:
You can add an API and a GUI in some minutes as the next step. See here: