0

When sqalchemy application starts, it automatically modifies database schema, so that database tables would reflect ORM models in python code. In my case it's very unwanted, because when I'm switching between different branches / writing new migrations / etc it's easy sometimes to accidentally run application in a "transitional" moment, so the DB gets broken by the attempt of sqlalchemy to "fix" the DB. By broken I mean detached from the last applied alembic_version, so that I can't even run a downgrade and have to fix it manually or use recovery.

So is there any native setting in sqalchemy to just raise / assert when real database schema does not match ORM models instead of automatically modifying the DB?

  • I'm not sure what you mean here. SQLAlchemy does not modify the database schema at start up. Can you clarify what you are doing? Do you call `Metadata.create_all` when your application starts? – snakecharmerb Aug 15 '22 at 13:05
  • If you wanted to, it wouldn't be too difficult to have your application get the value of alembic head and compare it with the contents of the alembic_version table. – snakecharmerb Aug 15 '22 at 13:06
  • Unfortunately, comapring alembic head with alembic_version doesn't cover some important scenarios. For example: I start modifying ORM, write new model class, etc. But I haven't yet started to write a migration. So my alembic head and alembic_version are equal. But models in python code are modified. So if I accidentally run application at this moment — sqlalchemy will modify db schema while alembic head and alembic_version will remain the same. And I will have problems later when will try to autogenerate a new migration or just apply a manually-written one. – usr7191 Aug 15 '22 at 15:22
  • Yes, Metadata.create_all() is called at start up. I suppose it was added at some early stages of development by someone (before I implemented alembic). Guess, that's what I was looking for and I can just remove it? It seems to work fine without it. Thanks! Maybe you would post it as an answer so I can check it as an accepted? – usr7191 Aug 15 '22 at 15:22

0 Answers0