google-code-export / dblinq2007

Automatically exported from code.google.com/p/dblinq2007
Other
0 stars 0 forks source link

Getting "Sequence contains more than one matching element" error when repeating foreign key for two different columns #250

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
NOTE: I also reported this bug here:
http://groups.google.com/group/dblinq-bugs/browse_thread/thread/15d0fe0141c284b0

--------------- Steps to reproduce ---------------

1. In SQLite, add these two tables to a database called "TestDb":

CREATE TABLE Person
(
    Id INTEGER PRIMARY KEY,
    Name TEXT NOT NULL
);

CREATE TABLE Match
(
    Id INTEGER PRIMARY KEY,
    WinnerPersonId INTEGER NOT NULL REFERENCES Person(Id),
    LoserPersonId INTEGER NOT NULL REFERENCES Person(Id)
);

2. Run this command in DOS command-line:

"C:\...\DbMetal.exe"
    /provider:Sqlite
    /conn:"Data Source = C:\...\TestDb.sqlite"
    /code:"C:\...\TestModel.cs"
    /namespace:TestProj.Models

--------------- Expected Result ---------------

Command runs with no errors.

--------------- Actual Result ---------------

Commands fails with this error:
DbMetal: Sequence contains more than one matching element

--------------- Discussion: ---------------

"REFERENCES Person(Id)" is repeated twice, but this makes perfect sense and
should not be considered an error (and it works fine if I use SqlServer
with SqlMetal). If I remove
the second REFERENCES clause, the error disappears.

--------------- System Specs: ---------------

Windows XP
Visual Studio 2008
SQLite
DbLinq 0.20.1

(Everything is updated to the latest service pack/version) 

Original issue reported on code.google.com by danspama...@gmail.com on 4 May 2010 at 6:47

GoogleCodeExporter commented 9 years ago
Someone created the following patch that seems to fix this issue. Just modify 
the
files mentioned here in the src and compile. Enjoy

Index: src/DbMetal/Generator/CodeDomGenerator.cs
===================================================================
--- src/DbMetal/Generator/CodeDomGenerator.cs   (revision 1408)
+++ src/DbMetal/Generator/CodeDomGenerator.cs   (working copy)
@@ -879,7 +879,7 @@

         CodeTypeMember CreateChangedMethodDecl(Column column)
         {
-            return CreatePartialMethod(GetChangedMethodName(column.Member));
+            return CreatePartialMethod(GetChangedMethodName(column.Member ??
column.Name));
         }

         static string GetChangingMethodName(string columnName)
@@ -889,7 +889,7 @@

         CodeTypeMember CreateChangingMethodDecl(Column column)
         {
-            return CreatePartialMethod(GetChangingMethodName(column.Member),
+            return CreatePartialMethod(GetChangingMethodName(column.Member ??
column.Name),
                     new
CodeParameterDeclarationExpression(ToCodeTypeReference(column), "value"));
         }

@@ -937,7 +937,7 @@

         static string GetStorageFieldName(Column column)
         {
-            return GetStorageFieldName(column.Storage ?? column.Member);
+            return GetStorageFieldName(column.Storage ?? column.Member ?? 
column.Name);
         }

         static string GetStorageFieldName(string storage)
Index: src/DbMetal/Generator/Implementation/Processor.cs
===================================================================
--- src/DbMetal/Generator/Implementation/Processor.cs   (revision 1408)
+++ src/DbMetal/Generator/Implementation/Processor.cs   (working copy)
@@ -138,7 +138,7 @@
             foreach (var association in table.Type.Associations)
             {
                 var otherType           = database.Tables.Single(t => t.Type.Name ==
association.Type).Type;
-                var otherAssociation    = otherType.Associations.Single(a => 
a.Type
== table.Type.Name && a.ThisKey == association.OtherKey);
+                var otherAssociation    = otherType.Associations.Single(a => 
a.Type
== table.Type.Name && a.ThisKey == association.OtherKey && a.OtherKey ==
association.ThisKey);
                 var otherColumn         = otherType.Columns.Single(c => c.Member ==
association.OtherKey);

                 if (association.CardinalitySpecified && association.Cardinality ==
Cardinality.Many && association.IsForeignKey)

Original comment by fama...@gmail.com on 18 May 2010 at 3:34

GoogleCodeExporter commented 9 years ago
I have verified this same bug against MySQL also.  Please apply this fix to the 
trunk!

Original comment by eric.bo...@gmail.com on 30 Jun 2010 at 12:37

GoogleCodeExporter commented 9 years ago
Based on fixing it myself (then finding this fix!) only the changes to 
Processor.cs seem necessary.  Not sure whether the CodeDomGenerator is need to 
fix something else?

Original comment by cool...@gmail.com on 8 Jul 2010 at 4:04

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Initially I got the "Sequence contains more than one matching element" error.
Then, after I applied the above metioned patch/fix(I only made changes to 
Processor.cs), I got another error "Sequence contains no matching elements".

Eventually I got it working(?), or at least to output a ".cs" file that 
projects my database, by "commenting out" the following line(inside the same 
foreach-loop as the previous fix).

(around line 135):
bool ValidateAssociations(Database database, Table table)
{
...
//var otherColumn = otherType.Columns.Single(c => c.Member == 
association.OtherKey);
...
}

Original comment by pmt.berg...@googlemail.com on 1 Aug 2010 at 7:00