How to check if a SQLAlchemy mapping matches the existing tables
up vote
3
down vote
favorite
I'm using SQLAlchemy's declarative system to define my mapping. Afterwards, I make sure the corresponding tables exist via
Base.metadata.create_all(engine, checkfirst=True)
If I afterwards change one of my declarative classes (for example by adding a column) and run the program again then that change is only detected when I try to commit a session including an instance of that modified class. That happens at a relatively late time during the program's runtime. I would prefer to detect that problem during startup so that I can fail early.
How can I explicitly ask SQLAlchemy to check whether the existing tables match my declarative mapping?
Note that I'm not necessarily looking for how to detect what has changed or how to perform the corresponding migration -- I just want to know whether my tables match my mapping or not.
python sqlalchemy
add a comment |
up vote
3
down vote
favorite
I'm using SQLAlchemy's declarative system to define my mapping. Afterwards, I make sure the corresponding tables exist via
Base.metadata.create_all(engine, checkfirst=True)
If I afterwards change one of my declarative classes (for example by adding a column) and run the program again then that change is only detected when I try to commit a session including an instance of that modified class. That happens at a relatively late time during the program's runtime. I would prefer to detect that problem during startup so that I can fail early.
How can I explicitly ask SQLAlchemy to check whether the existing tables match my declarative mapping?
Note that I'm not necessarily looking for how to detect what has changed or how to perform the corresponding migration -- I just want to know whether my tables match my mapping or not.
python sqlalchemy
I am not aware of a built in function that will compare your object mapping to your db schema. You may have to use sqlalchemy inspection along with an information_schema query to your db to create your own comparison function.
– benvc
Nov 11 at 0:12
1
You could have a look through the alembic source and see how they do it. If it's a long running script, maybe even trigger an alembic autogenerate at the beginning of your script and check the resulting revision for any changes that it has picked up.. no point reinventing the wheel!
– SuperShoot
Nov 11 at 8:40
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm using SQLAlchemy's declarative system to define my mapping. Afterwards, I make sure the corresponding tables exist via
Base.metadata.create_all(engine, checkfirst=True)
If I afterwards change one of my declarative classes (for example by adding a column) and run the program again then that change is only detected when I try to commit a session including an instance of that modified class. That happens at a relatively late time during the program's runtime. I would prefer to detect that problem during startup so that I can fail early.
How can I explicitly ask SQLAlchemy to check whether the existing tables match my declarative mapping?
Note that I'm not necessarily looking for how to detect what has changed or how to perform the corresponding migration -- I just want to know whether my tables match my mapping or not.
python sqlalchemy
I'm using SQLAlchemy's declarative system to define my mapping. Afterwards, I make sure the corresponding tables exist via
Base.metadata.create_all(engine, checkfirst=True)
If I afterwards change one of my declarative classes (for example by adding a column) and run the program again then that change is only detected when I try to commit a session including an instance of that modified class. That happens at a relatively late time during the program's runtime. I would prefer to detect that problem during startup so that I can fail early.
How can I explicitly ask SQLAlchemy to check whether the existing tables match my declarative mapping?
Note that I'm not necessarily looking for how to detect what has changed or how to perform the corresponding migration -- I just want to know whether my tables match my mapping or not.
python sqlalchemy
python sqlalchemy
asked Nov 10 at 22:16
Florian Brucker
5,08412653
5,08412653
I am not aware of a built in function that will compare your object mapping to your db schema. You may have to use sqlalchemy inspection along with an information_schema query to your db to create your own comparison function.
– benvc
Nov 11 at 0:12
1
You could have a look through the alembic source and see how they do it. If it's a long running script, maybe even trigger an alembic autogenerate at the beginning of your script and check the resulting revision for any changes that it has picked up.. no point reinventing the wheel!
– SuperShoot
Nov 11 at 8:40
add a comment |
I am not aware of a built in function that will compare your object mapping to your db schema. You may have to use sqlalchemy inspection along with an information_schema query to your db to create your own comparison function.
– benvc
Nov 11 at 0:12
1
You could have a look through the alembic source and see how they do it. If it's a long running script, maybe even trigger an alembic autogenerate at the beginning of your script and check the resulting revision for any changes that it has picked up.. no point reinventing the wheel!
– SuperShoot
Nov 11 at 8:40
I am not aware of a built in function that will compare your object mapping to your db schema. You may have to use sqlalchemy inspection along with an information_schema query to your db to create your own comparison function.
– benvc
Nov 11 at 0:12
I am not aware of a built in function that will compare your object mapping to your db schema. You may have to use sqlalchemy inspection along with an information_schema query to your db to create your own comparison function.
– benvc
Nov 11 at 0:12
1
1
You could have a look through the alembic source and see how they do it. If it's a long running script, maybe even trigger an alembic autogenerate at the beginning of your script and check the resulting revision for any changes that it has picked up.. no point reinventing the wheel!
– SuperShoot
Nov 11 at 8:40
You could have a look through the alembic source and see how they do it. If it's a long running script, maybe even trigger an alembic autogenerate at the beginning of your script and check the resulting revision for any changes that it has picked up.. no point reinventing the wheel!
– SuperShoot
Nov 11 at 8:40
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243974%2fhow-to-check-if-a-sqlalchemy-mapping-matches-the-existing-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I am not aware of a built in function that will compare your object mapping to your db schema. You may have to use sqlalchemy inspection along with an information_schema query to your db to create your own comparison function.
– benvc
Nov 11 at 0:12
1
You could have a look through the alembic source and see how they do it. If it's a long running script, maybe even trigger an alembic autogenerate at the beginning of your script and check the resulting revision for any changes that it has picked up.. no point reinventing the wheel!
– SuperShoot
Nov 11 at 8:40