From the documentation: Return exactly one result or raise an exception. This is from the function call. What I am wondering is that if the database engine is running somewhere else with communication via tcp (for example), can the machine running the SQLAlchemy application ever receive more than one result? …
Tag: sqlalchemy
SQLAlchemy Model.query.get use generic function to get latest id
Consider this snippet. I am trying to get the user with the latest id like this: It throws an error saying Boolean value of this clause is not defined How do I get the user with the largest id by using the model.query API? Answer I would solve it like: An alternative would be:
Elegant Way to Deal with Marshmallow Altering a SQLAlchemy Object
I have found myself in a situation that does not seem to have an elegant solution. Consider the following (pseudo-ish) REST API code I have A PUT endpoint that will create a new resource, then return the dump of that resource. An ORM object that has a filePath property. This must be stored as a relative path.…
Using SQLAlchemy 1.4 classical / imperative mapping style?
I am using the clean architecture & TDD development method in my Python project. Updating from SQLAlchemy 1.3 to SQLAlchemy 1.4 broke the ability to test against an in-memory Postgres DB, and I can’t find how to fix the problem. Following DDD principles, the project uses the new imperative mapping syntax …
Trying to use SQLAlchemy relationships
As an example, I have a cities table and a movie theaters table. A city has multiple movie theaters so: now i want to get all theaters for a city: This query throws error: not sure what I’m doing wrong? Answer Something like this should automatically do the join for you: And then the query would be Make…
User permission sqlalchemy.orm.exc.MultipleResultsFound: Multiple rows were found for one()
I can use below code to control the authority of menu 15,the role_id’s is_edit is 1, the use can change the data in that menu, or user doesn’t have access to change/edit data,but the code is ugly, and I only can control one menu. How to control all menu? Maybe they look like this? I tried to write…
Writing a pydantic object into a sqlalchemy json column
I’m looking for a way to have a pydantic object stored in a sqlalchemy json column. My attempts so far are being tripped up by a datetime field in the pydantic object. I feel like I’m missing something obvious. My first attempt was to simply serialise the result of .dict(). But this doesn’t …
SQLAlchemy SQL expression with JSONB query
I have the following property on a flask-sqlalchemy model. I want to make this approved_at property a sortable column in flask-admin, but apparently I need to convert this to a hybrid property using SQL expressions. I don’t know how to convert this query into a sqlalchemy SQL expression, since it’…
Self-referencing many-to-many relationship with an association object in SQLAlchemy
I’ve found examples for a self-referencing many-to-many relationship with an association table. How can I achieve the same using an association object? The code below is based on: How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute?…
Validation error while inputting a record into Postgres
I have created a table using the following query, Following is my models.py Following is my schema.py I use the following code to create an user, The problem is that I’m getting the following error, raise ValidationError(errors, field.type_) pydantic.error_wrappers.ValidationError: 1 validation error fo…