efcore icon indicating copy to clipboard operation
efcore copied to clipboard

ALTER COLUMN migration with column type change requires USING [PostgreSQL]

Open aspirinv opened this issue 5 years ago • 2 comments

Migration of type column type change generates script like:

ALTER TABLE "[table name]" ALTER COLUMN "[column name]" TYPE integer;

tha gets failed on the execution phase as it requires a hint:

ALTER TABLE "[table name]" ALTER COLUMN "[column name]" TYPE integer USING "[column name]"::integer;

EF Core version: 5.0.2 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET 5.0 Operating system: IDE: Visual Studio 2019 16.10.4

aspirinv avatar Jul 30 '21 06:07 aspirinv

Duplicate of https://github.com/npgsql/efcore.pg/issues/144

roji avatar Jul 30 '21 11:07 roji

@aspirinv PostgreSQL will seamless perform column type alterations where the conversion is straightforward and non-destructive; for example, if you change an int property to a string, the generated ALTER COLUMN will work just fine. However, wherever the data conversion isn't straightforward or could fail, PostgreSQL requires the conversion SQL to be specified explicitly, and EF makes no attempt to hide that or do that for you. You're encouraged to edit the scaffolded migration and replace the failing AlterTable operation with raw SQL containing the exact conversion SQL you want.

roji avatar Jul 30 '21 12:07 roji