1

Resolved

SchemaCompare: changing PK is written wrong

description

Hi,

I have a DB table, with a PK "kep" (integer).
I have a table in memory, same name, also with integer column "kep", but with another column "kep2" which shall be the primary key (instead of "kep").

Comparing the schemata writes a wrong ALTER TABLE statement, not considering the "kep2" but "kep".

I attached screenshots from the debugging statement. The wrong result is 4.png.

The problem I think occures in WrieConstraint(), which is not considering the constraint itself as it calls WritePrimaryKey() without parameters, see 3.png

file attachments

comments

Martinjw wrote Jan 14 at 10:48 AM

Yes, that's a bug! Fixed in commit 31412

Martinjw wrote Feb 4 at 6:37 PM

In v1.3

** Closed by Martinjw 04.02.2014 10:58

mausomaS wrote Feb 4 at 6:39 PM

Hi,

I'm sorry, it is not comletely fixed yet.
See my proposed changes in the file diff.zip

Martinjw wrote Feb 5 at 7:48 AM

From the diffs you changed the signature on ConstraintWriter,WritePrimaryKey() to optionally take the primary key constraint itself. Most calls pass null, except for the WriteConstraint method, which takes a constraint.

At the moment, and if null is passed in, WritePrimaryKey just reads the primary key from the table. Which should be what you want. WriteConstraint should be called with a constraint already on the table, but it is called by MigrationGenerator.AddConstraint, so a manually constructed model may not be linked up.

Rather than have a parameter that is normally null, I'd prefer to create explicit overloads for WritePrimaryKey, one as present, the other taking an arbitrary constraint (In .net 4 I'd use an optional parameter).

mausomaS wrote Feb 5 at 8:17 AM

You're completely correct.

The problem I had ist this code:
    public string WriteConstraint(DatabaseConstraint constraint)
    {
        switch (constraint.ConstraintType)
        {
            case ConstraintType.PrimaryKey:
                return WritePrimaryKey(constraint);
            case ConstraintType.UniqueKey:
                return WriteUniqueKey(constraint);
..

In all cases except PrimaryKey the constraint -Parameter is considered as a parameter in the Write-Function.
But not in ConstraintType.PrimaryKey, and thats where I ran into problems as the PK was not updated in DB by a schema diff.

I wanted to share the changes I made to get this to work with you to resolve this issue. Feel free to change it to fit better into your design.

Thanks a lot, a great work. I like it very much.

Martinjw wrote Feb 5 at 11:06 AM

I'll be making a commit later today

Martinjw wrote Feb 5 at 5:40 PM

Commit 31810