Skip to content

Commit

Permalink
#834 SQLite - Support multiple foreign keys. Thanks to statler
Browse files Browse the repository at this point in the history
  • Loading branch information
sjh37 committed May 28, 2024
1 parent 184e488 commit cc8fc96
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14906,7 +14906,7 @@ SELECT m.name AS FK_Table,
p.[from] AS FK_Column,
p.[table] AS PK_Table,
p.[to] AS PK_Column,
'FK_' || TRIM(m.name) || '_' || TRIM(p.[to]) as Constraint_Name,
'FK_' || TRIM(m.name) || '_' || TRIM(p.[from]) || '_' || TRIM(p.[to]) as Constraint_Name,
'main' as fkSchema,
'main' as pkSchema,
p.[to] AS primarykey,
Expand Down
22 changes: 13 additions & 9 deletions Generator.Tests.Integration/SingleDatabaseTestSQLite.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.IO;
using System.Threading;
using System;
using Efrpg;
using Efrpg.FileManagement;
using Efrpg.Templates;
using Generator.Tests.Common;
using NUnit.Framework;
using Efrpg.FileManagement;
using Microsoft.Data.Sqlite;
using NUnit.Framework;
using System.IO;

namespace Generator.Tests.Integration
{
Expand Down Expand Up @@ -77,12 +77,13 @@ CONSTRAINT [PK_Efrpg] PRIMARY KEY (Id)
CREATE TABLE EfrpgItems
(
Id INTEGER PRIMARY KEY,
EfrpgId INTEGER NOT NULL,
Test INT NOT NULL,
Id INTEGER PRIMARY KEY,
EfrpgId INTEGER NOT NULL,
ParentEfrpgId INTEGER NULL,
Test INT NOT NULL,
CreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (EfrpgId) REFERENCES [Efrpg] (Id)
ON DELETE CASCADE ON UPDATE NO ACTION
FOREIGN KEY (EfrpgId) REFERENCES [Efrpg] (Id) ON DELETE CASCADE ON UPDATE NO ACTION,
FOREIGN KEY (ParentEfrpgId) REFERENCES [Efrpg] (Id) ON DELETE CASCADE ON UPDATE NO ACTION
);
CREATE INDEX [IX_Efrpg] ON [Efrpg] ([TEXT3]);
Expand All @@ -108,6 +109,9 @@ INSERT INTO EfrpgItems (Id, EfrpgId, Test)
";
cmd.ExecuteNonQuery();
connection.Close();

Console.WriteLine($"Database: {Filename}");
Console.WriteLine($"ConnectionString: {ConnectionString}");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,15 +869,21 @@ public class Efrpg
// Reverse navigation

/// <summary>
/// Child EfrpgItems where [EfrpgItems].[EfrpgId] point to this entity (FK_EfrpgItems_Id)
/// Child EfrpgItems where [EfrpgItems].[EfrpgId] point to this entity (FK_EfrpgItems_EfrpgId_Id)
/// </summary>
public virtual ICollection<EfrpgItem> EfrpgItems { get; set; } // EfrpgItems.FK_EfrpgItems_Id
public virtual ICollection<EfrpgItem> EfrpgItems_EfrpgId { get; set; } // EfrpgItems.FK_EfrpgItems_EfrpgId_Id

/// <summary>
/// Child EfrpgItems where [EfrpgItems].[ParentEfrpgId] point to this entity (FK_EfrpgItems_ParentEfrpgId_Id)
/// </summary>
public virtual ICollection<EfrpgItem> EfrpgItems_ParentEfrpgId { get; set; } // EfrpgItems.FK_EfrpgItems_ParentEfrpgId_Id

public Efrpg()
{
Text8 = "Hello";
Int1 = 123;
EfrpgItems = new List<EfrpgItem>();
EfrpgItems_ParentEfrpgId = new List<EfrpgItem>();
EfrpgItems_EfrpgId = new List<EfrpgItem>();
}
}

Expand All @@ -886,15 +892,21 @@ public class EfrpgItem
{
public long Id { get; set; } // Id (Primary key)
public long EfrpgId { get; set; } // EfrpgId
public long? ParentEfrpgId { get; set; } // ParentEfrpgId
public long Test { get; set; } // Test
public DateTime? CreatedAt { get; set; } // CreatedAt

// Foreign keys

/// <summary>
/// Parent Efrpg pointed by [EfrpgItems].([EfrpgId]) (FK_EfrpgItems_Id)
/// Parent Efrpg pointed by [EfrpgItems].([EfrpgId]) (FK_EfrpgItems_EfrpgId_Id)
/// </summary>
public virtual Efrpg Efrpg_EfrpgId { get; set; } // FK_EfrpgItems_EfrpgId_Id

/// <summary>
/// Parent Efrpg pointed by [EfrpgItems].([ParentEfrpgId]) (FK_EfrpgItems_ParentEfrpgId_Id)
/// </summary>
public virtual Efrpg Efrpg { get; set; } // FK_EfrpgItems_Id
public virtual Efrpg ParentEfrpg { get; set; } // FK_EfrpgItems_ParentEfrpgId_Id
}

// ThisIsAView
Expand Down Expand Up @@ -967,11 +979,13 @@ public void Configure(EntityTypeBuilder<EfrpgItem> builder)

builder.Property(x => x.Id).HasColumnName(@"Id").HasColumnType("integer").IsRequired().ValueGeneratedOnAdd();
builder.Property(x => x.EfrpgId).HasColumnName(@"EfrpgId").HasColumnType("integer").IsRequired();
builder.Property(x => x.ParentEfrpgId).HasColumnName(@"ParentEfrpgId").HasColumnType("integer").IsRequired(false);
builder.Property(x => x.Test).HasColumnName(@"Test").HasColumnType("int").IsRequired();
builder.Property(x => x.CreatedAt).HasColumnName(@"CreatedAt").HasColumnType("datetime").IsRequired(false);

// Foreign keys
builder.HasOne(a => a.Efrpg).WithMany(b => b.EfrpgItems).HasForeignKey(c => c.EfrpgId).HasConstraintName("FK_EfrpgItems_Id");
builder.HasOne(a => a.Efrpg_EfrpgId).WithMany(b => b.EfrpgItems_EfrpgId).HasForeignKey(c => c.EfrpgId).HasConstraintName("FK_EfrpgItems_EfrpgId_Id");
builder.HasOne(a => a.ParentEfrpg).WithMany(b => b.EfrpgItems_ParentEfrpgId).HasForeignKey(c => c.ParentEfrpgId).HasConstraintName("FK_EfrpgItems_ParentEfrpgId_Id");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Generator/Readers/SQLiteDatabaseReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected override string ForeignKeySQL()
p.[from] AS FK_Column,
p.[table] AS PK_Table,
p.[to] AS PK_Column,
'FK_' || TRIM(m.name) || '_' || TRIM(p.[to]) as Constraint_Name,
'FK_' || TRIM(m.name) || '_' || TRIM(p.[from]) || '_' || TRIM(p.[to]) as Constraint_Name,
'main' as fkSchema,
'main' as pkSchema,
p.[to] AS primarykey,
Expand Down

0 comments on commit cc8fc96

Please sign in to comment.