3 ms·
I always found RedBean[1] to be an interesting ORM, despite being PHP based. It has a model where you put it in dev mode, and the calls you make create the dat
by tyingq 6y ago
I always found RedBean[1] to be an interesting ORM, despite being PHP based. It has a model where you put it in dev mode, and the calls you make create the database schema, including foreign relationships. Then you freeze it to remove the overhead. It does support more traditional flows as well. Is there something similar for Python?
[1] https://redbeanphp.com/index.php https://redbeanphp.com/index.php
- simonw 6y agoIt only works with SQLite, but my sqlite-utils Python library works a bit like that: you can feed it JSON objects and it will create a table schema to match: https://sqlite-utils.datasette.io/en/stable/python-api.html#creating-tables https://sqlite-utils.datasette.io/en/stable/python-api.html#... It has a CLI tool as well: https://sqlite-utils.datasette.io/en/stable/cli.html#inserting-json-data https://sqlite-utils.datasette.io/en/stable/cli.html#inserti...
- hantusk 6y agoNeat! you could prototype out the db in sqlite, and then have sqlalchemy introspect and write out a db-agnostic model from this. Happy to see the support around foreign key relationships and lookup tables here. Thanks!
- hantusk 6y agoYeah, see: https://dataset.readthedocs.io/en/latest/ https://dataset.readthedocs.io/en/latest/ It doesn't do foreign relationships though, guiding you to use sqlalchemy when you reach that level of complexity. It works for all databases supported by sqlalchemy though
- grenoire 6y agoSQLAlchemy has a sister project called Alembic for migrations, check it out https://alembic.sqlalchemy.org/en/latest/ https://alembic.sqlalchemy.org/en/latest/ Not really 'slap JSON get database' level, but well, close.
- jamil7 6y agoFrom using Alembic a little bit lately I've found it needs a lot more babysitting than for example Django's migrations. It's probably for the best as I end up reading and editing the migrations much more than I would in Django.
- grenoire 6y agoI definitely had my ass bit by Django migrations on deployment when I had cases like A->B->C (must be ran in order) but ran A->C through makemigrations, because I skipped the commit that is used to generate B. I did catch it only because I expected I would run into this problem, but yeah, easy to shoot yourself in the foot when you expect it to be 100% magic!
- StevePerkins 6y agoEvery major Java ORM has offered similar functionality for over 15 years now. At least in the Java community, I find that more and more shops are moving AWAY from letting their ORM library manage their database schema migrations. The current best practice is using something like Flyway or Liquibase. In which you place your DDL migration scripts (i.e. raw SQL) into source control, either with your application or externally. If it's with your application, then the application checks for new migrations and applies them at startup. If externally, then you do this yourself with a CLI tool. Either way, the system creates a table in your schema to track which migration scripts have been applied, along with hashes for each script. So the system can detect whether there's a new migration to perform, or throw an alert warning if someone retroactively changes an already-applied script. Of course, you're still welcome to leverage an ORM library in dev-mode to help you create that initial schema-setup script. It's just not great to rely on that approach for ongoing migrations in production. Having a trail of migration scripts in source control makes SO MUCH difference in reliability, and making it easier to stand up a new environment (or a local dev environment) that truly reproduces the state of your production schema.
- tyingq 6y agoI don't find Java ORMs terribly similar. They seem to lean heavily on annotations and not code, and creation of "table creation scripts", etc. RedBean doesn't import or export anything by default, it's "live". It's the live creation and modification I found interesting. I'm also not claiming that approach as some sort of panacea. It just happens to fit a "make another CRUD app" pattern I have to deal with a lot.