How to handle UUID in SQLModel for MSSQL ? (Conversion failed) #878
-
First Check
Commit to Help
Example CodeModel(shortened):
from pydantic import UUID4
class Customer_RoleBase(SQLModel):
roles_id: Optional[UUID4] = Field(primary_key=True, nullable=False)
Function to update DB(shortened):
def update_customer_role(role: Customer_Role) -> Customer_Role:
with Session(createDBEngine()) as session:
print(role.roles_id)
print(type(role.roles_id))
target = session.exec(select(Customer_Role).where(Customer_Role.roles_id == role.roles_id)).one()
if not target:
raise HTTPException(status_code=404, detail="Customer-Role not found") Description
I just try to get the matching role by its roles_id (UNIQUEIDENTIFIER) but it end´s in an error: Console Output: (see the print commands are first)120ba15d-61ea-40fa-8a39-dd0145d7ec7d sqlalchemy.exc.OperationalError: (pymssql._pymssql.OperationalError) (8169, b'Conversion failed when converting from a character string to uniqueidentifier.DB-Lib error message 20018 [SQL: SELECT customer_roles.roles_id, customer_roles.customer_id, customer_roles.system_id, customer_roles.salesorg_id, customer_roles.customer_role, customer_roles.role_number, customer_roles.full_name, customer_roles.first_name, customer_roles.last_name, customer_roles.partner_desc, customer_roles.default_partner, customer_roles.street, customer_roles.zip, customer_roles.city, customer_roles.country_code, customer_roles.email, customer_roles.phone, customer_roles.currency_code, customer_roles.payment_code, customer_roles.inco_terms_1, customer_roles.inco_terms_2, customer_roles.dist_channel, customer_roles.division The Column roles_id is of Type UUID4 and in the print command we can see its a real UUID and type of uuid.UUID but something after will convert it to something like a string without the hyphens. If i copy the SQL Statement to Azure Data Studio and run the query, it results in same error. I was searching and testing until now alot but could not find a correct solution. Operating SystemWindows Operating System DetailsNo response SQLModel Version0.0.11 Python Version3.11.0 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 16 replies
-
update: If I CAST the roles_id to UNIQUEIDENTIFIER the Where Clause will be ok, but later on the UPDATE Command will fail!
The next steps in updating the target will fail:
Error Message:
|
Beta Was this translation helpful? Give feedback.
-
The DB-Column´s default value =
The server_default property is just for information? |
Beta Was this translation helpful? Give feedback.
-
Thank you very much! |
Beta Was this translation helpful? Give feedback.
Actually the workaround solution is working for updating and creating new rows in DB-table.
@joachimhuet thanks for support!