Repository: dotnet/efcore
File: src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs (C#)

Code before:
                }

                // Skip autoincrement primary key columns that were newly added with default values
                // This prevents copying default values (like 0) to AUTOINCREMENT columns which would 
                // violate unique constraints when all rows have the same default value.
                var isAutoincrement = column.FindAnnotation(SqliteAnnotationNames.Autoincrement)?.Value as bool? == true;
                var isPrimaryKey = column.Table.PrimaryKey?.Columns.Contains(column) == true;
                
                if (isAutoincrement && isPrimaryKey)
                {
                    // Only skip if this appears to be a newly added column with default values
                    // We can detect this by checking if all columns except this one would be copied
                    // In that case, this column is likely new and should be auto-generated
                    
                    // Simple heuristic: if there's more than 1 column in the table, and one is autoincrement primary key,
                    // then preserve existing columns and skip autoincrement ones only if they seem new
                    var totalColumns = table.Columns.Count();
                    var nonAutoincrementColumns = table.Columns.Count(c => c.FindAnnotation(SqliteAnnotationNames.Autoincrement)?.Value as bool? != true);
                    
                    // If there are other non-autoincrement columns, this suggests the autoincrement column was added
                    // If this is the only column or all columns are autoincrement, preserve the values
                    if (totalColumns > 1 && nonAutoincrementColumns > 0)
                    {
                        continue; // Skip this newly added autoincrement column
                    }
                    // Otherwise, preserve the values (for single column tables or when converting existing column)
                }

                if (first)
                {
                    first = false;
                }
                else
                {
                    intoBuilder.Append(", ");
                    selectBuilder.Append(", ");
                }

                intoBuilder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(column.Name));

                var defaultValue = rebuildContext.AlterColumnsDeferred.TryGetValue(column.Name, out var alterColumnOperation)
                    && alterColumnOperation is { IsNullable: false, OldColumn.IsNullable: true }
                        ? alterColumnOperation.DefaultValue
                        : null;
                if (defaultValue != null)
                {
                    selectBuilder.Append("IFNULL(");
                }

                selectBuilder.Append(
                    Dependencies.SqlGenerationHelper.DelimitIdentifier(

Review comment (bug):
This heuristic is incorrect. Instead look for an `AddColumnOperation` in `operations` with matching table name, schema and column name. Also make the comment terser.

Code after:
                    continue;
                }

                // Skip autoincrement primary key columns that are being added in this migration
                var isAutoincrement = column.FindAnnotation(SqliteAnnotationNames.Autoincrement)?.Value as bool? == true;
                var isPrimaryKey = column.Table.PrimaryKey?.Columns.Contains(column) == true;
                
                if (isAutoincrement && isPrimaryKey)
                {
                    // Check if this column is being added in the current migration
                    var isNewColumn = migrationOperations.OfType<AddColumnOperation>()
                        .Any(op => op.Table == key.Table && op.Schema == key.Schema && op.Name == column.Name);
                    
                    if (isNewColumn)
                    {
                        continue; // Skip newly added autoincrement columns
                    }
                }

                if (first)
                {
                    first = false;
                }
                else
                {
                    intoBuilder.Append(", ");
                    selectBuilder.Append(", ");
                }

                intoBuilder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(column.Name));

                var defaultValue = rebuildContext.AlterColumnsDeferred.TryGetValue(column.Name, out var alterColumnOperation)
                    && alterColumnOperation is { IsNullable: false, OldColumn.IsNullable: true }
                        ? alterColumnOperation.DefaultValue
                        : null;
                if (defaultValue != null)
                {
                    selectBuilder.Append("IFNULL(");
                }

                selectBuilder.Append(
                    Dependencies.SqlGenerationHelper.DelimitIdentifier(
                        rebuildContext.RenameColumnsDeferred.TryGetValue(column.Name, out var renameColumnOperation)
                            ? renameColumnOperation.Name
                            : column.Name));

                if (defaultValue != null)
                {
                    var defaultValueTypeMapping = (column.StoreType == null
                            ? null
                            : Dependencies.TypeMappingSource.FindMapping(defaultValue.GetType(), column.StoreType))