Using SQLAlchemy and Django ORM in the same database

I have two applications that support the same database. The first has clients connecting over TCP and writing to db using SQLAlchemy. The second is a more typical webapp using Django. Both have read / write requirements.

I would like to combine the database access level, but choosing only SQLAlchemy or just Django is unattractive because:

  • I would like to use django auth, permissions and possibly third-party plugins that require Django ORM (correct me if I am wrong).
  • For the first application, using SQLAlchemy (so far) is much easier than trying to use Django ORM outside of a Django application - it is a TCP / IP application, not an HTTP / web application.

Are there any problems mixing these two ORMs in the same database?

On which system (Django, SQLA) should I create models, but with the help of some kind of introspection, for example Django inspectdb ?

+6
source share
1 answer

First, it’s not very difficult to use Django ORM outside of manage.py , WSGI, and other HTTP-related handlers. You can use any python script, but it needs some initialization ( example ).

Secondly, SQLA is a very powerful tool, and it can do things that are very difficult to achieve in Django ORM (for example, genuine polymorphism and polymorphic queries). If I had to choose, I would personally decide to use Django ORM as a platform for creating models, and then manually map them to SQLA, since it is much more flexible and, I hope, able to accept. What may not work in the opposite case.

Finally, since you can use Django ORM on both sides, and you just have to use Django ORM because of the plugins, I suggest abandoning SQLA. This is a powerful tool, but also quite complex. Having two different ORMs running in the same database can lead to unforeseen problems in the future and increase the complexity of your application, making it more difficult to maintain.

+6
source

Source: https://habr.com/ru/post/917220/


All Articles