6 ms·
I love SQLAlchemy and Alembic. I’ve done many migrations from other languages to Python and to Django controlled databases using these tools. When starting a n
by leetrout 6y ago
I love SQLAlchemy and Alembic. I’ve done many migrations from other languages to Python and to Django controlled databases using these tools.
When starting a new Go project this year I insisted we roll the database with Alembic so we would have a solid migration framework. I don’t regret it but the team has replaced it with Go Migrations with raw SQL and it looks like it’s going to be great but SQLAlchemy and Alembic will always be my go-to for important, timely database work when there isn’t already an ORM and/or migration tool in place.
Edit to add (see below): SQLAlchemy makes it easy to share access to things that aren’t controlled by Django but I did not make Django model or manager shims to hide the fact SQLAlchemy was in play.
- scrollaway 6y agoHave you ever ported a Django application to SQLAlchemy while leaving it in the Django ecosystem? Or used SQLAlchemy in Django in any way. I suppose that once you remove the orm, if you're not using forms etc there isn't much left but I'm curious how easy such a port can be.
- leetrout 6y agoShort answer: nope. As you identified everything is so tied to the models you aren’t left with much that makes Django worth using. I think there are old discussions on the Django core dev mailing list about supporting alternative ORMs like the template engines in a pluggable fashion but it would be so much work. I’ve actually been quite happy with the Django ORM and I’ve dropped out of it to do raw SQL when I’ve needed Postgres features that weren’t supported.
- skrtskrt 6y agoI am convinced (through experience) that overuse of the Django ORM (and Django REST Framework, which is tightly coupled to the ORM) is a pattern that leads to intractable ball-of-mud apps. There's so much stuff available for Django out of the box like auth, http/REST, CSRF, etc that it makes it really hard to justify not using Django at the start of a project. But the recommended/accepted Django patterns lead to pure misery down the road due to how coupled the app becomes. You can make a well-architected Django app, it just requires ignoring almost every common or recommended Django pattern.
- scrollaway 6y ago> it just requires ignoring almost every common or recommended Django pattern Indeed I'm already ignoring a lot of them. No forms or templates of any kind, nor any of the stuff that is built for that (messages framework etc); only using DRF; stick to Postgres-only (I don't make much use of the db abstractions), etc. The ORM is … nice, and has its limitations. I really just want to start using FastAPI by default but I'm concerned I'm setting myself up for a lot of busywork by skipping on such high-value niceties as the Django admin or DRF viewsets.
- ttymck 6y agoI think DRF Viewsets may be easy to replicate with the help of class based views from fastapi-utils[0] But I agree that Admin is still the missing killer feature from the fastapi ecosystem. But it looks like there is an emerging ORM/Admin combo heavily based on Django[1]. I've also considered (if you prefer SQLAlchemy) the possibility of instantiating a Flask/Flask-Admin wsgi app and mounting it on a running fastapi server, but haven't been able to confirm if that works yet. (EDIT: I just confirmed it does indeed work, and is rather painless.) [0] https://fastapi-utils.davidmontague.xyz/ https://fastapi-utils.davidmontague.xyz/ [1] https://github.com/long2ice/fastapi-admin https://github.com/long2ice/fastapi-admin
- collyw 6y agoMaybe I am just a god at writing Django, but I don't write ball of mud apps.
- iainctduncan 6y ago
- ericvanular 6y agoBig fan of SQLAlchemy & Alembic as well - I had originally assumed that these tools were for people using Flask and the like. Curious how you use it with Django? Django has a built in migration manager or so I thought
- leetrout 6y agoAh sorry that wasn’t clear. We had a PHP app with MySQL and built out our new user management and billing in Django with Postgres. I created SQLAlchemy models to map the PHP MySQL tables to Python, implemented the Django password hashing steps in PHP and let both apps access both databases as we slowly ported things to Django. It worked very, very well even given the complexity. And running migrations all from Python kept it all in one spot conceptually.
- welder 6y agoCame here to say the same thing. SQLAlchemy + Alembic is so powerful. Also, I think this crowd might like this blog I wrote about serializing SQLAlchemy models into JSON: https://wakatime.com/blog/32-flask-part-1-sqlalchemy-models-to-json https://wakatime.com/blog/32-flask-part-1-sqlalchemy-models-...
- amenod 6y agoI hate SQLAlchemy and Alembic.* I still use them both, because they are at least some kind of standard and SQLAlchemy plays with FastAPI very nicely - and I love API input validation and docs for free. Don't get me wrong - it's nice that there is some abstraction layer above the database. However, when it doesn't work the way you want it to, you need to find some workarounds for things that are otherwise trivial in SQL. Alembic... meh. Just give me the SQL and I will migrate database, no problem. But looking for the yet another syntax to perform the same thing is not my idea of fun. Not to mention that all migrations I ever did were linear - I can't imagine why someone would need dependency resolution in a db migration tool. Still, it is a kind of standard, so if you are working as part of a team... shrugs * edit: to be exact, I don't really hate hate them... I am just very frustrated with them from time to time. :)
- pm 6y agoI was using SQLAlchemy with FastAPI and databases, but still had issues with the lack of async in SQLAlchemy, even though it's supposed to be coming in 2.0. Ended up moving to Tortoise ORM, which is an async copy of Django ORM, but there are some odd design decisions, and I'm not a fan of the way it plays with pydantic. I've yet to try a migration with it too, so the jury's out whether it was a worthwhile decision.
- deleted 6y ago[deleted]