ExistsForeignKeyByFields

PublicDB.ExistsForeignKeyByFields(i interface{}, ft, rt, ff, rf string) (bool, error)

The ExistsForeignKeyByName method checks for the existence of the specified foreign-key constraint in the database.

The following go structs contain ‘sqac:’ tags declaring a foreign-key constraint between table column product.warehouse_id and table column warehouse.id. The resulting foreign-key constraint on table column product.warehouse_id would be named “fk_product_warehouse_id” based on the sqac foreign-key naming standard.

type Warehouse struct {
    ID       uint64 `db:"id" json:"id" sqac:"primary_key:inc;start:40000000"`
    City     string `db:"city" json:"city" sqac:"nullable:false;default:Calgary"`
    Quadrant string `db:"quadrant" json:"quadrant" sqac:"nullable:false;default:SE"`
}

type Product struct {
    ID          uint64 `db:"id" json:"id" sqac:"primary_key:inc;start:95000000"`
    ProductName string `db:"product_name" json:"product_name" sqac:"nullable:false;default:unknown"`
    ProductCode string `db:"product_code" json:"product_code" sqac:"nullable:false;default:0000-0000-00"`
    UOM         string `db:"uom" json:"uom" sqac:"nullable:false;default:EA"`
    // foreign-key declared against table "warehouse", column "id"
    WarehouseID uint64 `db:"warehouse_id" json:"warehouse_id" sqac:"nullable:false;fkey:warehouse(id)"`
}

Parameters

Parameter Description
i interface{} Accepts the go struct type of the table on which the foreign-key is thought to be declared. For example:If table column product.warehouse_id had a foreign-key constraint against table column warehouse.id, this parameter should be provided with go type Product{}.
ft string The database table name of the table on which the foreign-key is thought to be declared. For example:If table column product.warehouse_id is thought to have been assigned a foreign-key of table column warehouse.id, this parameter should be provided with “product”.
rt string The database table name of the table on which the foreign-key is thought to refer to. For example:If table column product.warehouse_id is thought to have been assigned a foreign-key of table column warehouse.id, this parameter should be provided with “warehouse”.
ff string The database name of the column on which the foreign-key is thought to have been declared. For example:If table column product.warehouse_id is thought to have been assigned a foreign-key of table column warehouse.id, this parameter should be provided with “warehouse_id”.
rf string The database name of the column on which the foreign-key is thought to refer to. For example:If table column product.warehouse_id is thought to have been assigned a foreign-key of table column warehouse.id, this parameter should be provided with “id”.

PublicDB.ExistsForeignKeyByFields Example

A sample call using the scenario described in the Parameters table follows:

type Warehouse struct {
    ID       uint64 `db:"id" json:"id" sqac:"primary_key:inc;start:40000000"`
    City     string `db:"city" json:"city" sqac:"nullable:false;default:Calgary"`
    Quadrant string `db:"quadrant" json:"quadrant" sqac:"nullable:false;default:SE"`
}

type Product struct {
    ID          uint64 `db:"id" json:"id" sqac:"primary_key:inc;start:95000000"`
    ProductName string `db:"product_name" json:"product_name" sqac:"nullable:false;default:unknown"`
    ProductCode string `db:"product_code" json:"product_code" sqac:"nullable:false;default:0000-0000-00"`
    UOM         string `db:"uom" json:"uom" sqac:"nullable:false;default:EA"`
    // foreign-key declared against table "warehouse", column "id"
    WarehouseID uint64 `db:"warehouse_id" json:"warehouse_id" sqac:"nullable:false;fkey:warehouse(id)"`
}
...
...
// check that the foreign-key exists by fields
fkExists, err := Handle.ExistsForeignKeyByFields(Product{}, "product", "warehouse", "warehouse_id", "id")
...
...

Notes

In practice, this method is not very different from PublicDB.ExistsForeignKeyByName. It could be enhanced to use the actual string name of the foreign-key constraint as a parameter rather than using the sqac foreign-key name standard to assemble the name to be checked.