英文:
golang pq sql driver: pq: invalid input syntax for type uuid error type
问题
我正在尝试处理Postgres错误。
如果用户发送了无效的uuid - Postgres会返回一个带有消息的错误:pq: invalid input syntax for type uuid:...
所以,我想检查这个错误,如果错误等于无效的输入语法错误 - 我想显示给客户端404错误,而不是返回服务器错误。
但是我找不到Postgres库中返回的错误的定义错误类型...
有人能告诉我错误是在哪里返回的吗?
英文:
I'm trying to handle Postgres error.
If user sends invalid uuid - Postgres returns an error with message: pq: invalid input syntax for type uuid:...
So, i want to check that error and if the error equals to the invalid input syntax error - i would like to show to the client 404 error, rather than returning server error.
But i can't find the defined error type for the returned error in Postgres library...
Can someone suggest me, where is the error returned?
答案1
得分: 1
在查询中,你可以将 uuid
列转换为 text
类型。以下是两种方法:
SELECT * FROM user WHERE id::text = 'sometext'
SELECT * FROM user WHERE text(id) = 'sometext'
这里也有相关讨论:
https://stackoverflow.com/a/46494463/2316115
PostgreSQL 类型转换的文档在这里:
https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS
英文:
In the query you can cast the uuid
column to text
. Here are two ways to do that:
SELECT * FROM user WHERE id::text = 'sometext'
SELECT * FROM user WHERE text(id) = 'sometext'
Also discussed here:
https://stackoverflow.com/a/46494463/2316115
PostgreSQL Type Casts are documented here:
https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论