Skip to content

Npgsql 6+ issues with null DateTime parameter types #3291

Closed
@hazzik

Description

@hazzik

Hi!
I am sorry, it seems theese changes leads to an error in some cases.

Versions:

  • NHibernate 5.4.2
  • Npgsql 7.0.2

The named query:

	<sql-query name="MyQuery" cache-mode="normal" read-only="true">
	  <query-param type="DateTime" name="DateOfSearch"/>
	  <return-scalar type="Int64" column="SomeID"/>
	  <![CDATA[select ...
	        from ...
	        where ((  :DateOfSearch is null ) or (( t.DT1 <= :DateOfSearch) and ( t.DT2 >= :DateOfSearch )))
	        and ... ;]]>
	</sql-query>
	</hibernate-mapping>

Call the query:

	DateTime? dateOfSearch = null; // set null value here -- !!!!
	query.SetParameter("DateOfSearch", dateOfSearch, NHibernateUtil.DateTime);
	var result = query.List<long>();

NHibernate log:

	2023-04-21 23:01:51,433 [1] DEBUG NHibernate.Type.DateTimeType - binding null to parameter: 0
	2023-04-21 23:01:51,433 [1] DEBUG NHibernate.Type.DateTimeType - binding null to parameter: 1
	2023-04-21 23:01:51,434 [1] DEBUG NHibernate.Type.DateTimeType - binding null to parameter: 2
	2023-04-21 23:01:51,445 [1] DEBUG NHibernate.SQL - select ...
	        from ...
	        where ((  :p0 is null ) or (( mmh.DT_Mount <= :p0) and ( mmh.DT_Dismount >= :p0 ))); :p0 = NULL [Type: Object (0:0:0)]  -- !!!!
	...
	2023-04-21 23:01:51,623 [1] ERROR NHibernate.AdoNet.AbstractBatcher - Could not execute query: select ...

dbParam.DBType is empty, and Npgsql sends Prepare command to PostgreSQL with no parameter type specified, but PostgreSQL also can not guess the type from this query and value.
It works correctly, if I set non-null parameter value.

Originally posted by @olga-sonix in #3064 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions