Search code examples
c#foreign-keysadodbadox

COMException when adding ForeignKey to the table using ADOX for AccessDB


I get this exception: Invalid field definition 'NalogID' in definition of index or relationship. Exception is fired when I append the table in the code below to the catalog

I can't figure out what parameter goes in the 3rd place (this is what Visual Studio shows Object column = Type.missing) on the following line of code:

tableNalogRelacija.Keys.Append("ForeignKey", ADOX.KeyTypeEnum.adKeyForeign, "NalogID", "Nalog", "NalogID");

I followed this example: how to add foreign key to access table using adox

Here is full code sample:

// generira tabelu Relacije
            ADOX.Table tableNalogRelacija = new ADOX.Table();
            ADOX.Key tableKey4 = new Key();
            ADOX.Column idColumn4 = new Column();
            // Define column with AutoIncrement features
            idColumn4.Name = "ID";
            idColumn4.Type = ADOX.DataTypeEnum.adInteger;
            // Set ID as primary key
            tableKey4.Name = "Primary Key";
            tableKey4.Columns.Append("ID");
            tableKey4.Type = KeyTypeEnum.adKeyPrimary;
            //Create the table and it's fields. 
            tableNalogRelacija.Name = "NalogRelacija";
            tableNalogRelacija.Columns.Append(idColumn4);
            tableNalogRelacija.Columns.Append("MjestoPolaska", ADOX.DataTypeEnum.adVarWChar, 50);
            tableNalogRelacija.Columns.Append("MjestoDolaska", ADOX.DataTypeEnum.adVarWChar, 30);
            tableNalogRelacija.Columns.Append("Udaljenost", ADOX.DataTypeEnum.adInteger);
            // Dodavanjeg stranog ključa NalogID
            tableNalogRelacija.Columns.Append("NalogID", ADOX.DataTypeEnum.adInteger);
            tableNalogRelacija.Keys.Append("ForeignKey", ADOX.KeyTypeEnum.adKeyForeign, "NalogID", "Nalog", "NalogID");

~ ChenChi


Solution

  • Turns out I wasn't getting Primary Keys so it threw non uniqe index field Exception then I made them like this:

                // CREATE TABLE RELACIJA
                ADOX.Table tableNalogRelacija = new ADOX.Table();
                tableNalogRelacija.Name = "NalogRelacija";
    
                // APPEND PRIMARY KEY
                tableNalogRelacija.Columns.Append("IDRelacija", ADOX.DataTypeEnum.adInteger);
                tableNalogRelacija.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary, "IDRelacija");
    
                // APPEND COLUMNS
                tableNalogRelacija.Columns.Append("MjestoPolaska", ADOX.DataTypeEnum.adVarWChar, 50);
                tableNalogRelacija.Columns.Append("MjestoDolaska", ADOX.DataTypeEnum.adVarWChar, 30);
                tableNalogRelacija.Columns.Append("Udaljenost", ADOX.DataTypeEnum.adInteger);
                // ADD FOREIGN KEY NalogID
                tableNalogRelacija.Columns.Append("NalogID", ADOX.DataTypeEnum.adInteger);
                tableNalogRelacija.Keys.Append("ForeignKey_NalogRelacija", ADOX.KeyTypeEnum.adKeyForeign, "NalogID", "Nalog", "IDNalog");
    

    And now everything works fine