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)"`
}
Parameter | Description |
---|---|
i interface{} | Accepts the go struct type of the table from 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{}. |
fkn string | The name of the foreign-key being checked. For example:If table column product.warehouse_id has a foreign-key constraint against table.column warehouse.id, by convention this parameter should contain: “fk_product_warehouse_id”. |
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)"`
}
...
...
// determine the standard foreign-key name (expect "fk_product_warehouse_id")
fkn, err = common.GetFKeyName(Product{}, "product", "warehouse", "warehouse_id", "id")
...
...
// check that the foreign-key exists by name
fkExists, err := Handle.ExistsForeignKeyByName(Product{}, fkn)
...
...