Search code examples
pythonflaskflask-sqlalchemyflask-mysql

Is it possible to mix 2 ORMS in same web app?


So is it possible to mix 2 ORM's in same web app,and if so how optimal would it be ? Why so? - I'm working on a web app in flask using flask-mysqldb and I came to a point where I need to implement an auth system, and on flask-mysqldb there's no secure way to do it. - With that said now I'm trying to implement flask-security but it only works on flask-sqlalchemy so I'm trying to mix sqlalchemy with mysqldb and before that I want to know if it's optimal and if it works.That would lead to using user auth along sqlalchemy and other data to mysqldb.Thanks!


Solution

  • It's possible, but not recommended. Consider this:

    • Half of your app will not benefit from anything a proper ORM offers
    • Adding a field to the table means editing raw SQL in many places, and then changing the model.
      Don't forget to keep them in sync.

    Alternatively, you can port everything that uses raw mysqldb to use SQLAlchemy:

    • Need to add a field to your table? Just change the model in one place.
    • Don't like SQL queries that ORM generates for you? You still have a low-level control over this.