Skip to content

Incorrect Association Creation with Composite Primary Keys #3810

Description

@DirDonBin

Describe your issue

Hello. There is a problem related to composite primary keys. When we are using scaffolding via the CLI, attribute generation for associations is incorrect. Duplicated key values appears in the attribute. the LoadWith<> method does not work because of that .

Steps to reproduce

  1. Create DB with 2 tables:

    • First table with name PKTable and colums Id, AnotherId
    • Second table with name FKTable and colums PKTableId, PKTableAnotherId
  2. Create Composite PK for PKTable with columns Id and AnotherId

  3. Create Composite FK for FKTable with columns PKTableId and PKTableAnotherId to PKTable to Id and AnotherId

  4. Run CLI command dotnet linq2db scaffold -i database.json

  5. Fill DB some random data

  6. Write any query with LoadWith<> Method

  7. Get null on association property

        [Table("PKTable")]
	public class PkTable
	{
		[Column("Id"       , IsPrimaryKey = true, PrimaryKeyOrder = 0, IsIdentity = true, SkipOnInsert = true, SkipOnUpdate = true)] public int Id        { get; set; } // integer
		[Column("AnotherId", IsPrimaryKey = true, PrimaryKeyOrder = 1                                                             )] public int AnotherId { get; set; } // integer

		#region Associations
		/// <summary>
		/// FKTable_PKTableAnotherId_PKTableId _fkey backreference
		/// </summary>
		[Association(ThisKey = nameof(Id) + "," + nameof(Id), OtherKey = nameof(FkTable.PkTableId) + "," + nameof(Id))]
		public List<FkTable> FkTablePkTableAnotherIdPkTableIdFkeys { get; set; } = null!;
		#endregion
	}
        [Table("FKTable")]
	public class FkTable
	{
		[Column("Id"              , IsPrimaryKey = true, IsIdentity = true, SkipOnInsert = true, SkipOnUpdate = true)] public int  Id               { get; set; } // integer
		[Column("PKTableId "                                                                                        )] public int? PkTableId        { get; set; } // integer
		[Column("PKTableAnotherId"                                                                                  )] public int? PkTableAnotherId { get; set; } // integer

		#region Associations
		/// <summary>
		/// FKTable_PKTableAnotherId_PKTableId _fkey
		/// </summary>
		[Association(ThisKey = nameof(PkTableId) + "," + nameof(PkTableId), OtherKey = nameof(PkTable.Id) + "," + nameof(PkTableId))]
		public PkTable? PkTableAnotherIdPkTableIdFkey { get; set; }
		#endregion
	}

Environment details

Linq To DB version: 4.3.0

Database (with version): PostgreSQL 14

ADO.NET Provider (with version): NpgSql 7.0.0-rc.1

Operating system: Windows 10

.NET Version: 7.0

Solution suggestion

image
image

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions