Skip to content

Commit 29bbc32

Browse files
committed
Support PG18 virtual generated columns
Closes #3568
1 parent 4bc916f commit 29bbc32

File tree

2 files changed

+52
-180
lines changed

2 files changed

+52
-180
lines changed

src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,10 +1872,11 @@ protected override void ComputedColumnDefinition(
18721872
throw new NotSupportedException("Computed/generated columns aren't supported in PostgreSQL prior to version 12");
18731873
}
18741874

1875-
if (operation.IsStored != true)
1875+
if (operation.IsStored is not true && _postgresVersion < new Version(18, 0))
18761876
{
18771877
throw new NotSupportedException(
1878-
"Generated columns currently must be stored, specify 'stored: true' in "
1878+
"Virtual (non-stored) generated columns are only supported on PostgreSQL 18 and up. " +
1879+
"On older versions, specify 'stored: true' in "
18791880
+ $"'{nameof(RelationalPropertyBuilderExtensions.HasComputedColumnSql)}' in your context's OnModelCreating.");
18801881
}
18811882

@@ -1894,12 +1895,17 @@ protected override void ComputedColumnDefinition(
18941895
builder
18951896
.Append(" GENERATED ALWAYS AS (")
18961897
.Append(operation.ComputedColumnSql!)
1897-
.Append(") STORED");
1898+
.Append(")");
18981899

1899-
if (!operation.IsNullable)
1900+
if (operation.IsStored is true)
19001901
{
1901-
builder.Append(" NOT NULL");
1902+
builder.Append(" STORED");
19021903
}
1904+
1905+
if (!operation.IsNullable)
1906+
{
1907+
builder.Append(" NOT NULL");
1908+
}
19031909
}
19041910

19051911
#pragma warning disable 618

0 commit comments

Comments
 (0)