3

I've begin building a web application using Flask and Flask-SQLAlchemy. My experience with building web applications is very limited so I apologize before hand if I am being ambiguous.

The database for the web application is already built by another program running on the server. So all the tables are already predefined in the database. My web app and the other program running on the server will have read & write access to the database.

Recently I've decided to restructure my application and cater toward the model-view-controller schema, similar to Django, and to use blueprints for making things more manageable and neater. Similar to this https://github.com/mitsuhiko/flask/wiki/Large-app-how-to

...
app_name/
    model.py
    view.py
    controller.py
    __init__.py
...

So I've done a few tutorials and read up on Flask-SQLAlchemy but what I can't make sense for the life of me is how do I define my models in the application if my database is already predefined by another program??. and How can I use the SQLAlchemy ORM with a predefined database?

Previously, before I decided to restructure, I just used Pyhton's sqlite3 library for grabbing stuff from the database and displaying it on the web page. It wasn't to pretty and the way I was structuring things things would have gotten out of hand fast.

tereško
  • 58,060
  • 25
  • 98
  • 150
drewski
  • 111
  • 2
  • 10

1 Answers1

3

Does the other program also use sqlalchemy so that you can lift/share its models? If not, you could try reflection: http://docs.sqlalchemy.org/en/latest/core/reflection.html Or this experimental automap: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html

And your question wasn't about this, but sqlite3 is a file database, not a client-server database. Not saying you will, but you may run into concurrency issues:

sqlite3 concurrent access

http://www.sqlite.org/lockingv3.html

The OS and filesystem plays a role in this.

Community
  • 1
  • 1
dcr
  • 967
  • 2
  • 9
  • 20
  • For a small app, these will work. But (and this is to OP) it will be messy and hard to maintain, because if any database changes are required, you'll need to make them in two places. A better model is to have only one writer to the DB and expose an API for everything else. You can't use SQLAlchemy with that model, sadly. – Rachel Sanders Jun 27 '14 at 21:22