0

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...

davidism
  • 121,510
  • 29
  • 395
  • 339
Clément F
  • 3,535
  • 6
  • 18
  • 26
  • 1
    an ORM, like SQLAlchemy represents records from the db in the form of objects. These objects need to corresponds to some class so that they can exhibit entity specific behavior. – sid-m Apr 12 '20 at 17:01
  • You do not necessarily need to define models explicitly, you could automap/reflect or use core/raw queries. – Ilja Everilä Apr 13 '20 at 07:19

1 Answers1

2

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.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103