A new unique column is being added to a table. This column's value comes from a C# random code generator method, but for existing rows I need to rely on Postgres.
I have the following EF Core migration:
protected override void Up(MigrationBuilder migrationBuilder)
{
var randomCodeSql = @"
SELECT string_agg (substr('ABCDEFGHJKLMNPQRSTUVWXYZ123456789', ceil (random() * 33)::integer, 1), '')
FROM generate_series(1, 6)";
migrationBuilder.AddColumn<string>(
name: "reference_code",
table: "invitations",
type: "character varying(12)",
maxLength: 12,
nullable: false,
defaultValueSql: randomCodeSql);
migrationBuilder.CreateIndex(
name: "ix_invitations_reference_code",
table: "invitations",
column: "reference_code",
unique: true);
}
The raw SQL is from this answer.
I get this error:
0A000: cannot use subquery in DEFAULT expression
Is there a code-first solution to this problem?