Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using both SQLAlchemy and Django ORM on the same database

I have two apps that both access the same database. The first has clients connecting via TCP and writes to the db using SQLAlchemy. The second is a more typical webapp using Django. Both have read/write requirements.

I would like to unify the database access layer, but picking just SQLAlchemy or just Django is unattractive because:

  1. I would like to use django auth, permissions, and maybe third party plugins, which require the Django ORM (correct me if I'm wrong).
  2. For the first app, using SQLAlchemy (so far) is much simpler than trying to use the Django ORM outside of a Django app - it is a TCP/IP server app, not a HTTP/web app.

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

In which system (Django, SQLA) should I create the models, vs using some kind of introspection such as Django inspectdb?

like image 408
user1431368 Avatar asked Jun 01 '12 21:06

user1431368


People also ask

Can we use SQLAlchemy ORM in Django?

Both Django and SQLAlchemy can be used with MySQL, PostgreSQL, Oracle and SQLite. If you're using MSSQL, you should go for SQLAlchemy, as it's fully supported by it and you'll find more information and documentation about it.

Is SQLAlchemy better than Django ORM?

Agree with all previous answers: yes, SQLAlchemy is really more powerful than Django ORM, gives you a real control over your SQL and is very explicit. But 90% of our work is just simple create/read/update/delete or finding object by id.

Can I use Django ORM standalone?

Django is one of the popular python frameworks; critiques have argued that it is a bloated framework. The truth of the matter is that it is very modularized, and each of the components () can be independently used.

Does Django web framework uses SQLAlchemy as Object Relational Mapper?

Questions & Answers › Category: Programming Language › Django web framework uses SQLAlchemy as Object-relational mapper. Django web framework uses SQLAlchemy as Object-relational mapper.


1 Answers

Firstly - it's not very hard to use Django ORM outside a manage.py, WSGI handlers and other HTTP related stuff. You can use it any python script, but it needs some initialization (example).

Secondly - SQLA is a very powerfull tool and it's able to do stuff which is very hard to achive in Django ORM (like genuine polymorphism and polymorphic queries). If I had to choose, I'd personally choose to use Django ORM as a platform to create models, then manually map them in SQLA since it is much more flexibile and hopefully will be able to adopt. Which may not work in the opposite case.

Finally, since you can use Django ORM on both sides, and you just have to use a Django ORM because of the plugins, I suggest to abandon the SQLA. It's a powerfull tool, but also rather complicated. Having two different ORMs operating on one database may result in unexpected problems in the future and increases the complexity of your app, so it'll be harder to maintenance.

like image 52
Wojciech Żółtak Avatar answered Nov 15 '22 16:11

Wojciech Żółtak