Clément F
Clément F

Reputation: 3955

Why do I need to define models in a Flask + SQLAlchemy app using an existing database?

I'm new to Flask and SQLalchemy and I'm currently trying to develop an API connecting to an existing database. I understand models are useful to create a new database, and new tables, but I do not understand what makes it neccesary for me to define models since my tables already exist ?

I read this question, and I don't understand what makes it necessary to define models if I can simply query my database? Maybe I did not grasp the purpose of models...

Upvotes: 0

Views: 666

Answers (1)

Laurent LAPORTE
Laurent LAPORTE

Reputation: 22952

You need to define a model if you use an ORM: because an ORM needs to do the mapping between the database model (the tables) and the object model (the classes).

You don't have to do that if you use a DB-API because you do direct calls to the database. Here is an exemple with SQLite: https://docs.python.org/3.8/library/sqlite3.html.

Upvotes: 2

Related Questions