You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Statement order in down-migration is incorrect #14030
protectedoverridevoidUp(MigrationBuildermigrationBuilder){migrationBuilder.CreateSequence(name:"seq_bi_id",startValue:15000000000L);migrationBuilder.AlterColumn<long>(name:"bi_id",table:"animals",nullable:false,defaultValueSql:"NEXT VALUE FOR seq_bi_id",oldClrType:typeof(long));}protectedoverridevoidDown(MigrationBuildermigrationBuilder){migrationBuilder.DropSequence(name:"seq_bi_id");migrationBuilder.AlterColumn<long>(name:"bi_id",table:"animals",nullable:false,oldClrType:typeof(long),oldDefaultValueSql:"NEXT VALUE FOR seq_bi_id");}
Generated by this code:
conststringbiIds="seq_bi_id";modelBuilder.HasSequence<long>(biIds).StartsAt(15000000000).IncrementsBy(1);modelBuilder.Entity<AnimalEntity>().Property(a =>a.BiId).HasDefaultValueSql($"NEXT VALUE FOR {biIds}");
In this scenario, the down migration fails as the execution order is the same as with the Up migration. The sequence cannot be removed, as it is still used as default value sql in the animals table.
It would make sense if by default the generated order is flipped. There are probably exceptions with dependencies etc, but in general reversing the order should always be correct (you want to revert statements, which are coincidentally grouped in a migration). It would at least solve the problem here.
Similar to #12726. We need to be smarter about columns with HasDefaultValueSql and HasComputedColumnSql. Although without actually parsing the SQL, we'll never be perfect.
The workaround is to manually re-order the operations after scaffolding.
Activity
bricelam commentedon Nov 28, 2018
Similar to #12726. We need to be smarter about columns with
HasDefaultValueSqlandHasComputedColumnSql. Although without actually parsing the SQL, we'll never be perfect.The workaround is to manually re-order the operations after scaffolding.
bricelam commentedon Oct 9, 2019
Looking closer, I don't think we should fix this. At a high level, the order we use is:
This is to enable replacing database objects with new ones. I think that if we try to fix this specific case, it will cause other scenarios to fail.
Short of parsing the SQL (a complexity we desperately wish to avoid), I don't think we can adequately fix this.