Skip to content
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

mysql IdentityColumn not primary key bug #34679

Open
winxp2002 opened this issue Sep 15, 2024 · 1 comment
Open

mysql IdentityColumn not primary key bug #34679

winxp2002 opened this issue Sep 15, 2024 · 1 comment

Comments

@winxp2002
Copy link

winxp2002 commented Sep 15, 2024

in mysql 5.5.7 ,

table define :
pc
{
id (auto increment)
char_name (primary key )
user_id
}
when I db.Migrate() in empty database
app run throw a error

MySqlConnector.MySqlException:“Incorrect table definition; there can be only one auto column and it must be defined as a key”

I think this is related to the internal creation order of efcore, first creating tables (including primary keys and auto increment information), and then executing constraint statements
When MySQL creates a table, it does not include unique index information for self increasing columns

mysql sql scripts ,script is correct create table

CREATE TABLE `pc`  (
  `char_id` int(11) NOT NULL AUTO_INCREMENT,
  `char_name` varchar(16) NOT NULL DEFAULT '',
  `user_id` varchar(16) NOT NULL DEFAULT '',
  PRIMARY KEY (`char_name`) USING BTREE,
  INDEX `user_id`(`user_id`) USING BTREE,
  INDEX `char_id`(`char_id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 1  ROW_FORMAT = Dynamic;

model

public partial class PcEntity
{
    public int char_id { get; set; }
    public string char_name { get; set; } = null!;
    public string user_id { get; set; } = null!;
}

configuration

public class PcEntityConfiguration : IEntityTypeConfiguration<PcEntity>
{
    public void Configure(EntityTypeBuilder<PcEntity> builder)
    {
        builder.ToTable("pc");
        builder.HasKey(e => e.CharName).HasName("PRIMARY");
        builder.HasIndex(e => e.char_id, "char_id");
        builder.HasIndex(e => e.user_id, "user_id");
        builder.Property(e => e.char_id).ValueGeneratedOnAdd().HasColumnType("int(11)");
        builder.Property(e => e.user_id).HasMaxLength(16);
        builder.Property(e => e.char_name).HasMaxLength(16);
    }
}

Add-Migration Initial

public partial class Initial : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
             name: "pc",
             columns: table => new
             {
                 char_name = table.Column<string>(type: "varchar(16)", maxLength: 16, nullable: false),
                 char_id = table.Column<int>(type: "int(11)", nullable: false).Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                 user_id = table.Column<string>(type: "varchar(16)", maxLength: 16, nullable: true)
             },
             constraints: table =>
             {
                 table.PrimaryKey("PRIMARY", x => x.char_name);
             });

        migrationBuilder.CreateIndex(
             name: "char_id",
             table: "pc",
             column: "char_id");

        migrationBuilder.CreateIndex(
            name: "user_id",
            table: "pc",
            column: "user_id");
    }
}
	<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
	<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.0">
		<PrivateAssets>all</PrivateAssets>
		<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	</PackageReference>
	<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0" />
@AndriySvyryd
Copy link
Member

Does it work if you don't call ValueGeneratedOnAdd(), create a migration then add the ValueGeneratedOnAdd() call and create another migration?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants