Problems with Sqlachemy. Can't understand the difference
up vote
0
down vote
favorite
I have the following code
def update(project_id, code, description):
if project_id is None:
raise exception
with session_handler() as session:
project = session.query(Project).filter_by(project_id=project_id).first()
project_upd = session.query(Project).filter_by(project_id=project_id)
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
project_upd.update(data)
So if I replace project_upd.update(data)
with project.update(data)
it gives the following error
Attribute Error: Project object has no attribute update.
How I can use only one variable?
python sql sqlalchemy
add a comment |
up vote
0
down vote
favorite
I have the following code
def update(project_id, code, description):
if project_id is None:
raise exception
with session_handler() as session:
project = session.query(Project).filter_by(project_id=project_id).first()
project_upd = session.query(Project).filter_by(project_id=project_id)
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
project_upd.update(data)
So if I replace project_upd.update(data)
with project.update(data)
it gives the following error
Attribute Error: Project object has no attribute update.
How I can use only one variable?
python sql sqlalchemy
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the following code
def update(project_id, code, description):
if project_id is None:
raise exception
with session_handler() as session:
project = session.query(Project).filter_by(project_id=project_id).first()
project_upd = session.query(Project).filter_by(project_id=project_id)
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
project_upd.update(data)
So if I replace project_upd.update(data)
with project.update(data)
it gives the following error
Attribute Error: Project object has no attribute update.
How I can use only one variable?
python sql sqlalchemy
I have the following code
def update(project_id, code, description):
if project_id is None:
raise exception
with session_handler() as session:
project = session.query(Project).filter_by(project_id=project_id).first()
project_upd = session.query(Project).filter_by(project_id=project_id)
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
project_upd.update(data)
So if I replace project_upd.update(data)
with project.update(data)
it gives the following error
Attribute Error: Project object has no attribute update.
How I can use only one variable?
python sql sqlalchemy
python sql sqlalchemy
edited Nov 8 at 8:02
Ilja Everilä
22.4k33459
22.4k33459
asked Nov 8 at 7:20
Mila Bogomolova
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
Though the Project
model is omitted, it is clear that it has no method update()
– which is the norm. The difference is that project
is bound to a Project
object, and project_upd
to a Query
object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.
Your options are:
- Issue an UPDATE statement only and check if it matched any rows, raise if not.
- Fetch the row / object for update, check if it existed and raise if not, do the update.
I assume that session_handler()
commits, if no exception was raised. If that is not the case, add an explicit session.commit()
as necessary.
1. Single UPDATE statement
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
row_count = session.query(Project).
filter_by(project_id=project_id).
update(data)
if not row_count:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
2. Fetch and update
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
# Fetch FOR UPDATE so that no concurrent updates may proceed in between
# getting the `Project` instance and actually updating it.
project = session.query(Project).
filter_by(project_id=project_id).
with_for_update().
first()
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
for attr, val in data.items():
setattr(project, attr, val)
Locking with FOR UPDATE
might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Though the Project
model is omitted, it is clear that it has no method update()
– which is the norm. The difference is that project
is bound to a Project
object, and project_upd
to a Query
object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.
Your options are:
- Issue an UPDATE statement only and check if it matched any rows, raise if not.
- Fetch the row / object for update, check if it existed and raise if not, do the update.
I assume that session_handler()
commits, if no exception was raised. If that is not the case, add an explicit session.commit()
as necessary.
1. Single UPDATE statement
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
row_count = session.query(Project).
filter_by(project_id=project_id).
update(data)
if not row_count:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
2. Fetch and update
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
# Fetch FOR UPDATE so that no concurrent updates may proceed in between
# getting the `Project` instance and actually updating it.
project = session.query(Project).
filter_by(project_id=project_id).
with_for_update().
first()
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
for attr, val in data.items():
setattr(project, attr, val)
Locking with FOR UPDATE
might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.
add a comment |
up vote
2
down vote
Though the Project
model is omitted, it is clear that it has no method update()
– which is the norm. The difference is that project
is bound to a Project
object, and project_upd
to a Query
object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.
Your options are:
- Issue an UPDATE statement only and check if it matched any rows, raise if not.
- Fetch the row / object for update, check if it existed and raise if not, do the update.
I assume that session_handler()
commits, if no exception was raised. If that is not the case, add an explicit session.commit()
as necessary.
1. Single UPDATE statement
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
row_count = session.query(Project).
filter_by(project_id=project_id).
update(data)
if not row_count:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
2. Fetch and update
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
# Fetch FOR UPDATE so that no concurrent updates may proceed in between
# getting the `Project` instance and actually updating it.
project = session.query(Project).
filter_by(project_id=project_id).
with_for_update().
first()
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
for attr, val in data.items():
setattr(project, attr, val)
Locking with FOR UPDATE
might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.
add a comment |
up vote
2
down vote
up vote
2
down vote
Though the Project
model is omitted, it is clear that it has no method update()
– which is the norm. The difference is that project
is bound to a Project
object, and project_upd
to a Query
object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.
Your options are:
- Issue an UPDATE statement only and check if it matched any rows, raise if not.
- Fetch the row / object for update, check if it existed and raise if not, do the update.
I assume that session_handler()
commits, if no exception was raised. If that is not the case, add an explicit session.commit()
as necessary.
1. Single UPDATE statement
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
row_count = session.query(Project).
filter_by(project_id=project_id).
update(data)
if not row_count:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
2. Fetch and update
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
# Fetch FOR UPDATE so that no concurrent updates may proceed in between
# getting the `Project` instance and actually updating it.
project = session.query(Project).
filter_by(project_id=project_id).
with_for_update().
first()
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
for attr, val in data.items():
setattr(project, attr, val)
Locking with FOR UPDATE
might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.
Though the Project
model is omitted, it is clear that it has no method update()
– which is the norm. The difference is that project
is bound to a Project
object, and project_upd
to a Query
object. In other words the former represents a single row in the table mapped to an object, while the latter represents a query against that table.
Your options are:
- Issue an UPDATE statement only and check if it matched any rows, raise if not.
- Fetch the row / object for update, check if it existed and raise if not, do the update.
I assume that session_handler()
commits, if no exception was raised. If that is not the case, add an explicit session.commit()
as necessary.
1. Single UPDATE statement
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
row_count = session.query(Project).
filter_by(project_id=project_id).
update(data)
if not row_count:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
2. Fetch and update
def update(project_id, code, description):
if project_id is None:
raise exception
data = _build_update_data(code, description)
if not data:
raise ValueError("No code or description provided")
with session_handler() as session:
# Fetch FOR UPDATE so that no concurrent updates may proceed in between
# getting the `Project` instance and actually updating it.
project = session.query(Project).
filter_by(project_id=project_id).
with_for_update().
first()
if project is None:
raise ProjectDoesntExist(f"Project {project_id} does not exist.")
for attr, val in data.items():
setattr(project, attr, val)
Locking with FOR UPDATE
might be a bit unnecessary in this case, since the new values don't seem to depend on previous state of the object. Still, it is something to keep in mind.
edited Nov 8 at 12:31
answered Nov 8 at 11:26
Ilja Everilä
22.4k33459
22.4k33459
add a comment |
add a comment |
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%2f53203039%2fproblems-with-sqlachemy-cant-understand-the-difference%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