Skip to content

Collation lost when changing type from varchar to text #3476

Closed

Description

@Verdurakh

Bug description

When changing a type of a column from varchar to text the previously set collation seems to be lost.

The migration adds a Alter Column statement specifying the same collation for both old and new.

My expected sql code would have been something like

ALTER TABLE address ALTER COLUMN tag TYPE text COLLATE case_insensitive;
which if done manually works and keeps the collation but this is not what is happening in the migration.

orginial Entity Configuration
	builder.Property(address => address.Tag).HasMaxLength(50);            
When removing the HasMaxLength(5) the below migration is created.

public class AddressEntity 
{
	
	public required string? Tag { get; set; }
}



migrationBuilder.AlterColumn<string>(
                name: "tag",
                table: "address",
                type: "text",
                nullable: true,
                collation: "case_insensitive",
                oldClrType: typeof(string),
                oldType: "character varying(50)",
                oldMaxLength: 50,
                oldNullable: true,
                oldCollation: "case_insensitive");

After this the collation for this column is null

Activity

  1. Verdurakh commented on Feb 27, 2025

    @Verdurakh
    Author

    Note that if I set the oldCollation = null it works as expected

  2. self-assigned this
    on Feb 27, 2025
  3. added this to the 9.0.4 milestone on Feb 27, 2025
  4. Verdurakh commented on Feb 28, 2025

    @Verdurakh
    Author

    seems to be from here
    https://github.com/npgsql/efcore.pg/blob/main/src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
    Line 459

            if (type != oldType || newCollation != oldCollation)
            {
                builder
                    .Append(alterBase)
                    .Append("TYPE ")
                    .Append(type);
    
                if (newCollation != oldCollation)
                {
                    builder.Append(" COLLATE ").Append(DelimitIdentifier(newCollation ?? "default"));
                }
    
                builder.AppendLine(";");
            }
    
  5. added a commit that references this issue on Feb 28, 2025
    fa0bf7e
  6. roji commented on Feb 28, 2025

    @roji
    Member

    Thanks @Verdurakh, have pushed a fix in #3476 and will backport to 9.0.4.

  7. added 5 commits that reference this issue on Feb 28, 2025
    9f4b731
    e327d6b
    4bf3248
    9c747fd
    bf45245
  8. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions