efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

[2.0.0 NuGet released version] Migrations with default DateTimeOffset values throw exceptions

Open samppis opened this issue 9 years ago • 7 comments

After upgrading from 1.1.1 to 2.0.0 EF, migrations containg a default DateTimeOffset value throw an exception during database update.

Example migration: migrationBuilder.AddColumn<DateTimeOffset>( name: "CreatedOn", table: "Users", nullable: false, defaultValue: new DateTimeOffset(new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), new TimeSpan(0, 0, 0, 0, 0)));

It results in SQL like this: ALTER TABLE "Users" ADD "CreatedOn" timestamptz NOT NULL DEFAULT 01/01/0001 00:00:00 +00:00;

Which throws exception when being run: Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "00" at Npgsql.NpgsqlConnector.<DoReadMessage>d__148.MoveNext()

samppis avatar Sep 19 '17 10:09 samppis

Hi. I have met something similar. Looks like it does not like default DateTimeOffset or DateTime value. Try to replace it with nullable type and null default value. This behavour occur when i am trying save item without initializing DateTime field or setting DateTime from Nullable without value. Can you provide information about your versions. I can not reproduce it on my machine, but my teammates complain about it on two different machines. This situation is terrible. This problem appeared when I have replaced timestamp by timestamptz in cases where value value would set to something like 01/01/0001 00:00:00

aldari avatar Nov 28 '17 11:11 aldari

I think I'm getting the same issue, but with a TimeSpan in migrations. I am using Npgsql.EntityFrameworkCore.PostgreSQL version 2.0.1:

<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="2.0.1" />

It appears to have been an issue before, and fixed by pull request https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/pull/218

My offending code in my migration:

migrationBuilder.AddColumn<TimeSpan>(
                name: "SomeDurationThingo",
                table: "SomeTable",
                nullable: false,
                defaultValue: new TimeSpan(0, 0, 0, 0, 0))

This generates the error:

Applying migration '20170620040507_AddDurationThing'.

Failed executing DbCommand (10ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE "SomeTable" ADD "SomeDurationThingo" interval NOT NULL DEFAULT 00:00:00;

... massive stack trace ...

42601: syntax error at or near ":"

This seems to have surfaced as an issue after our jump from dotnet core 1.1 to 2.0, so that probably means the issue cropped up in the dotnet 2.0+ supporting packages.

crozone avatar Feb 15 '18 01:02 crozone

Is there a chance that this problem will be solved in the near future? Or maybe I could help somehow? With the current version (2.0.1), it is not possible to even create a database for a new project using Identity. I can not apply the migration with the same error. Probably because one of the tables uses DateTimeOffset. P.S. It also did not work in the project with the version of .NET Core 1.1 and the same version of the library.

Kazbek avatar Apr 01 '18 22:04 Kazbek

Apologies to all for letting this linger for so long.

Version 2.0.0 of the provider did have a serious issue with handling timestamp with time zone (mapped by default when you have a DateTimeOffset CLR property). This bug was fixed in #218 and released with version 2.0.1 - so people using the latest version should not be seeing any DateTimeOffset-related issues - can you please check and confirm?

However, I did check and TimeSpan suffers from a very similar issue - I've fixed this in #358 and plan to release 2.0.2 with it.

However, I'd like to make sure everything works well before releasing 2.0.2. Can you guys please:

  1. Reconfirm that there are no DateTimeOffset-related issues in 2.0.1
  2. Ideally compile the 2.0.2 branch (hotfix/2.0.2) and confirm that there are no other issues (e.g. TimeSpan is fixed)

roji avatar Apr 02 '18 05:04 roji

FYI instead of compiling yourself, you can also simply add the Npgsql stable feed to your NuGet.Config and install 2.0.2-257.

Any feedback would be greatly appreciated!

roji avatar Apr 02 '18 05:04 roji

I checked the release version 2.0.1. It seems to be working fine. I'm used this code to use DateTimeOffset in migration: public class ApplicationUser : IdentityUser { [Required] public DateTimeOffset RegistrationTime { get; set; } } Correctly updated the database and performed the operations of writing and reading from these fields. As it turned out, the problem was in connecting to a remote server. I tested everything on the local. Perhaps, due to the fact that on my computer (Windows 10) and a remote server (Windows Server 2012R2) the system language is russian. Since even pgAdmin does not connect, issuing the error: 'utf8' codec can't decode byte 0xc2 in position 0: invalid continuation byte If I work locally on the server, everything works fine. Search queries on the Internet indicate that the change of settings in applications can help. So I think my problem solved.

P.S. Migration exception if somebody needed:

Entity Framework Core 2.0.2-rtm-10011 initialized 'ApplicationDbContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: MigrationsAssembly=DataCore 
Npgsql.PostgresException (0x80004005): 28000: � pg_hba.conf ��� ������ ��� ���������� "SomeIP", ������������ "postgres", ���� "YourDb", SSL ����.

Kazbek avatar Apr 05 '18 15:04 Kazbek

Closing as duplicate of #218, which was fixed in 2.0.1.

If anyone is still having in issue on 2.0.1 or 2.0.2, please post back here and we'll revisit.

roji avatar Apr 22 '18 09:04 roji