Base solution for your next web application
Open Closed

Creating Composite Key Index on Foreign Key - MySQL #8349


User avatar
0
maharatha created

I have a table which has two foreign keys. So when I add Foreign Keys it adds two indexes in the table.

Now when I try to create an Unique Index using the two foreignkeys, it is trying to drop the indexes created by adding a column as foreign key.

On doing so the Migration doesn't run as it is unable to drop an index created by adding teh foreign key.

Cannot drop index 'IX_xyz_approval_rule_xyz_id': needed in a foreign key constraint

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

4 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Are you using EF 6.x or EF Core? Is it the sql server database?

    By the way please share the code of the relevant entity.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    maharatha created

    I am using EFCore and My SQL

    public class Po_approval_assignment : Entity , IMayHaveTenant
        {
    		
    
    		[StringLength(Po_approval_assignmentConsts.Maxcreated_byLength, MinimumLength = Po_approval_assignmentConsts.Mincreated_byLength)]
    		public virtual string created_by { get; set; }
    		
    		public virtual DateTime? created_date { get; set; }
    		
    		public virtual DateTime? last_modified_date { get; set; }
    		
    		[StringLength(Po_approval_assignmentConsts.Maxmodified_byLength, MinimumLength = Po_approval_assignmentConsts.Minmodified_byLength)]
    		public virtual string modified_by { get; set; }
    		
    		[ForeignKey("po_Approval_Rule")]
    		public virtual long po_approval_rule_id { get; set; }
    
    		public Po_approval_rule po_Approval_Rule { get; set; }
    
    		[ForeignKey("po_Reviewer")]
    		public virtual long po_reviewer_id { get; set; }
    
    		public Po_reviewer po_Reviewer { get; set; }
    		
    		public virtual int? version { get; set; }
    
            [Column("tenant_id")] public int? TenantId { get; set; }
    	}
    

    I am trying to create a composite primary key using po_approval_rule_id and po_reviewer_id

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    maliming created
    Support Team

    Are you using ef core provider MySql.Data.EntityFrameworkCore or Pomelo.EntityFrameworkCore.MySql?

    Maybe related this issue: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/678

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    ismcagdas created
    Support Team

    This issue is closed because of no recent activity. Please create a new issue if you are still having this problem.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)