[odb-users] MS SQL Foreign Key Error

Dean Throop DThroop at PacificCabinets.com
Sun Mar 31 19:46:41 EDT 2013


I would greatly appreciate anyone's assistance in understanding how I am screwing up this implementation.  I am new to ODB, and not a C++ expert.

My primary objective is to change a to-one relationship to a pragmatically determined entity that already exists in the database.  My schema is locked as this is an existing production database, for which I am working on a local, temp copy.

I have a Products table with a to-one relationship to an ItemTypes table.  The ItemTable's primary key is an int "ID" with two additional fields.  The Products table holds the pertinent ItemTypes table ID in the ItemTypeID field and there is a FK constraint.

I am providing basic stubs of what I think is pertinent, but I will be happy to zip anything anyone else would like to see.

Please note that my classes are fully encapsulated.

Thanks in advance, and please feel free to let me if I am doing something else stupid.

main
{
  ...  
   transaction t(iesDB->begin ());

    Product *aProduct(iesDB->load<Product>(pID));
    std::vector<ItemType> productTypes;
    odb::result<ItemType> r(iesDB->query<ItemType>(query<ItemType>::isProduct==true));
    for(odb::result<ItemType>::iterator i(r.begin ());i!=r.end ();++i)
    {
        productTypes.push_back (*i);
    }

    aProduct->ConfigureProduct (productTypes); //does a couple of things, then executes the SetProductType(productTypes)
    iesDB->update(*aProduct);

    t.commit ();
}

#pragma db object table("ItemTypes")
class ItemType
{
public:
    ItemType();
    ~ItemType();
    const std::string& GetName() const;
    bool IsAProduct() const;
private:
    friend class odb::access;
#pragma db id auto
    int id;
    std::string name;
    bool isProduct;
};

#pragma db object table ("Products")
class Product
{
public:
    Product();
    ~Product();
    bool ConfigureProduct(const std::vector<ItemType> &productTypes);
private:
    friend class odb::access;
#pragma db not_null column("ItemTypeID")
    ItemType* productType;
    bool SetProductType(const std::vector<ItemType> &productTypes);
};

bool Product::SetProductType(const std::vector<ItemType> &productTypes)
{
        if(GetProductType ()!=currentProductType)
        {
            for(std::vector<ItemType>::const_iterator i(productTypes.begin ());i!=productTypes.end ();++i)
            {
                if(i->GetName()==currentProductType)
                {
                    ItemType theValue=*i;
                    productType=&theValue;
                    break;
                }
            }
        }
}

Error message:
terminate called after throwing an instance of 'odb::mssql::database_exception' what(): 547(23000)[Microsoft][SQL Server Native Client 11.0][SQL Server] The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Products_ItemTypes". The conflict occurred in database "PCI_IES_DB", table "dbo.ItemTypes", column 'ID'.

Dean



More information about the odb-users mailing list