npgsql: "Cannot write DateTime with Kind=Unspecified" is confusing

Using 6.0.2, we receive an error:

Cannot write DateTime with Kind=Unspecified to PostgreSQL type ‘timestamp with time zone’, only UTC is supported. Note that it’s not possible to mix DateTimes with different Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.

The table has solely columns of type timestamp without time zone. From other discussions I understand that for storing UTC, the type should be timestamp with time zone.

We will make sure the delivery of the data is with kind changed from Unspecified to UTC for the future.

However, I find the error message confusing since the types do not align between message and actual column types.

Suggestion is to finetune the error message, since it will probably be essential for a fast adaption of the Npgsql 6 driver.

column data types

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 29 (12 by maintainers)

Most upvoted comments

@monty241 the error message does not indicate that your PostgreSQL column type is wrong - Npgsql has no knowledge of the column type you’re inserting into, etc. It says that you’ve asked Npgsql to send an Unspecified DateTime as a PG timestamptz type, e.g. by setting NpgsqlDbType.TimestampTz on your parameter; so the mismatch isn’t between the value and the column, but rather between the value and your parameter type.

Can you provide the actual C# code triggering this? Is it because you’re mixing DateTimes with different kinds in an array and trying to send that?

@fernandocristan I don’t have details or a code sample from you, but it’s very likely that you’re writing a DateTime to the database, which Npgsql sends as timestamp to PostgreSQL, not as date; the type of the column in the database doesn’t matter here - Npgsql has no idea about your column type when sending the parameter (PG converts the timestamp that Npgsql sends to date when writing to the column).

You need to tell Npgsql to send a date. The recommended way is to switch to the DateOnly type introduced in .NET 6.0. Otherwise you can explicitly set NpgsqlDbType on the parameter to NpgsqlDbType.Date (see Dapper custom parameter types).

You may also be interested in the Npgsql.EnableLegacyTimestampBehavior compat flag

I am interested in avoiding it 😉

Thanks for your answer. Helpful!

This change is so frustrating.

When using DateTimeOffset.UtcNow + timestamp with time zone + specifying the parameter as NpgsqlDbType.TimestampTz

It still throws

System.InvalidCastException: Cannot write DateTime with Kind=UTC to PostgreSQL type ‘timestamp without time zone’, consider using ‘timestamp with time zone’. Note that it’s not possible to mix DateTimes with different Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.

The error makes no sense. I’m not using without time zone I’m using with time zone

This is the first time I’ve hit a real roadblock with npgsql and this change seems bonkers to me.