Check out a simple design for a modular flag with multiple instances of SQLAlchemy ()

My application has a plan that needs to be modular and uses its own database models. I am using a factory application template.

Two problems that I often encounter with such a project structure are:

  • Cyclic import.
  • Inability to go through a db object to drawings in a clean way so that they can create their own database models.

To avoid passing the db object around, now I started creating a new database object ( db = SQLAlchemy() ) inside the drawings themselves.

It works! It also avoids some problems with cyclic import, since this project now uses its own instance of db . My questions are: what problems will lead to this design? I am using gunicorn with a gevent working.

Here is a sample code showing how my models and factory are structured:

blueprints.status.models.py

 from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Status(db.Model): __tablename__ = 'states' id = db.Column(db.Integer, primary_key=True) job_id = db.Column(db.Integer) status = db.Column(db.String(120)) 

models.py

 from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Job(db.Model): __tablename__ = 'states' id = db.Column(db.Integer, primary_key=True) state = db.Column(db.String(120) 

factory.py

 from .blueprints.status.models import db as status_db # blueprint db from .blueprints.status.routes import status_handler # blueprint handler from .models import db as root_db # root db from flask import Flask def create_app(): app = Flask(__name__) # Create database resources. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////path/to/app.db' root_db.init_app(app) status_db.init_app(app) with app.app_context(): root_db.create_all() status_db.create_all() # <--- What problems will this lead to? # Register blueprint routes. app.register_blueprint(status_handler, url_prefix="/status") return app 
-one
python flask design-patterns flask-sqlalchemy
Dec 24 '17 at 19:26
source share
1 answer

Let's start with the import loop problem: this is not a problem. You just need to properly organize the module architecture. Some high-quality graphics to demonstrate: enter image description here just beautiful

Or just an example of github . Puff - loop disappearance issue has disappeared

Now about the design of many db objects

I would say that the main problem of this design is that it solves the problems that tried to tell you that you are doing something wrong and creating real ones:

  • Now you need to create a db instance in each project, not just one. I got the impression that each db object creates its own database connection, and in theory you might run into a limit ( for example, the heroic has a limit of 20 connections for non-payers ).
  • More import operations (SQLAlchemy for each project) they are expensive.
  • More code. I like this feeling from a DRY wiki :
DRY violations are usually referred to as WET decisions, which are usually taken for “writing everything twice,” “we like to type,” or “spending all our time."
  1. It’s more difficult to work with other people. When you work in a team where any suspicious code creates pain and suffering, the realization comes that you need to keep "wtf per minute" at lower values. enter image description here
0
Dec 29 '17 at 10:17
source share



All Articles