Hasura原生查询调用时日期字符串转DateTime失败,本地执行正常求因
Hasura原生查询API调用时出现日期转换错误的原因
我在Hasura中创建了如下原生查询:
SELECT tblPhOrderSummary.Id as OrderId, tblPhOrderSummary.CreatedOn as OrderDate, tblItems.ItemCode as ItemCode, tblPhOrderProducts.ItemDescription, tblItems.ModelCode AS ModelCode, tblItems.Barcode AS Barcode, tblPhOrderProducts.LOT AS LOT, '' AS Batch, tblPhOrderProducts.Expiry AS Expiry, tblItems.SalesPrice AS UnitSalesPrice, tblPhOrderProducts.Instock AS InStock, tblPhOrderProducts.CurrentStock AS CurrentStock, tblPhOrderProducts.OrderQuantity AS OrderQuantity FROM tblPhOrderProducts INNER JOIN tblItems ON tblPhOrderProducts.ItemCode = tblItems.ItemCode INNER JOIN tblPhOrderSummary ON tblPhOrderProducts.OrderId = tblPhOrderSummary.Id WHERE tblPhOrderSummary.CustomerId = '{{CustomerId}}' AND tblPhOrderSummary.CreatedOn BETWEEN '{{StartDate}}' AND '{{EndDate}}';
在Azure Data Studio中执行该查询可正常返回结果,但通过Postman调用Hasura API时,却返回错误:
Conversion failed when converting date and/or time from character string.
问题原因
- 参数格式不匹配数据库解析规则:数据库的
CreatedOn是日期/时间类型,Azure Data Studio中手动输入的日期字符串符合数据库要求格式(如YYYY-MM-DD),但API调用时传递的日期格式(如MM/DD/YYYY、带非标准分隔符的格式)无法被数据库正确解析,导致转换失败。 - Hasura无自动类型转换:Hasura原生查询中的
{{StartDate}}和{{EndDate}}会直接将传入的参数值以字符串形式拼入查询,不会自动转换为日期类型。如果API请求传递的参数没有以数据库可识别的日期字符串格式传入,就会触发转换错误。 - 未指定参数类型:在Hasura创建原生查询时,没有为
StartDate和EndDate明确指定日期类型参数,导致Hasura默认以字符串类型处理参数,数据库无法完成隐式类型转换。
内容的提问来源于stack exchange,提问作者Nizal Sha
相关产品推荐
相关产品推荐

