Skip to content

PrecisionAttribute on Decimal parameter causes false positive for PendingModelChangesWarning exception #3521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Luk164 opened this issue Apr 14, 2025 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@Luk164
Copy link

Luk164 commented Apr 14, 2025

Adding PrecisionAtribute to a Decimal parameter causes false positive for PendingModelChangesWarning exception.

Example:

[Precision(9)]
public decimal? ScalingFactor { get; set; }

Creating a migration works normally, but trying to apply it will result in PendingModelChangesWarning. Creating new migration afterwards results in an AlterColumn call for ScalingFactor that does not actually change anything.

Result of 1st migration:

public partial class ScaleFactorAddedAgain : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterDatabase()
            .OldAnnotation("Npgsql:PostgresExtension:pg_trgm", ",,");

        migrationBuilder.AddColumn<decimal>(
            name: "scaling_factor",
            table: "parameter",
            type: "numeric(9)",
            precision: 9,
            nullable: true);
    }

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "scaling_factor",
            table: "parameter");

        migrationBuilder.AlterDatabase()
            .Annotation("Npgsql:PostgresExtension:pg_trgm", ",,");
    }
}

Result of a subsequent migration (no other changes made to model):

    public partial class ScaleFactorAddedAgain2 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<decimal>(
                name: "scaling_factor",
                table: "parameter",
                type: "numeric(9)",
                precision: 9,
                nullable: true,
                oldClrType: typeof(decimal),
                oldType: "numeric(9,0)",
                oldPrecision: 9,
                oldNullable: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<decimal>(
                name: "scaling_factor",
                table: "parameter",
                type: "numeric(9,0)",
                precision: 9,
                nullable: true,
                oldClrType: typeof(decimal),
                oldType: "numeric(9)",
                oldPrecision: 9,
                oldNullable: true);
        }
    }

Commenting out the annotation, creating a new migration and setting the precision manually works as expected.

@roji roji self-assigned this Apr 14, 2025
@roji roji added the bug Something isn't working label Apr 14, 2025
@roji roji added this to the 8.0.12 milestone Apr 14, 2025
@roji
Copy link
Member

roji commented Apr 14, 2025

Thanks, I can see this happening.

As a workaround, simply specify the scale as well:

[Precision(9, 0)]
public decimal? ScalingFactor { get; set; }

@Luk164
Copy link
Author

Luk164 commented Apr 17, 2025

Thanks, I can see this happening.

As a workaround, simply specify the scale as well:

[Precision(9, 0)]
public decimal? ScalingFactor { get; set; }

Thanks, that worked, and it also made me realize that I swapped meanings for precision and scale. I thought precision was for decimal count

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants