Documentation ¶
Overview ¶
Package constraint provides types and methods to support foreign-key relationshipd between database tables.
Only simple keys are supported, which can be integers, strings or any other suitable type. Compound keys are not supported.
Index ¶
- type CheckConstraint
- type Consequence
- type Constraint
- type Constraints
- type FkConstraint
- func (c FkConstraint) ConstraintSql(q quote.Quoter, name pgxapi.TableName, index int) string
- func (c FkConstraint) GoString() string
- func (c FkConstraint) NoCascade() FkConstraint
- func (c FkConstraint) OnDelete(consequence Consequence) FkConstraint
- func (c FkConstraint) OnUpdate(consequence Consequence) FkConstraint
- func (c FkConstraint) RefersTo(tableName string, column string) FkConstraint
- func (c FkConstraint) RelationshipWith(child pgxapi.TableName) Relationship
- type FkConstraints
- type Reference
- type Relationship
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CheckConstraint ¶
type CheckConstraint struct {
Expression string
}
CheckConstraint holds an expression that refers to table columns and is applied as a precondition whenever a table insert, update or delete is attempted. The CheckConstraint expression is in SQL.
func (CheckConstraint) ConstraintSql ¶
ConstraintSql constructs the CONSTRAINT clause to be included in the CREATE TABLE.
func (CheckConstraint) GoString ¶
func (c CheckConstraint) GoString() string
type Consequence ¶
type Consequence string
Consequence is the action to be performed after updating or deleting a record constrained by foreign key.
const ( // unspecified option is available but its semantics vary by DB vendor, so it's not included here. NoAction Consequence = "no action" Restrict Consequence = "restrict" Cascade Consequence = "cascade" SetNull Consequence = "set null" SetDefault Consequence = "set default" Delete Consequence = "delete" // not MySQL )
func (Consequence) Apply ¶
func (c Consequence) Apply(pfx, action string) string
Apply constructs the SQL sub-clause for a consequence of a specified action. The prefix is typically arbitrary whitespace.
type Constraint ¶
type Constraint interface { // ConstraintSql constructs the CONSTRAINT clause to be included in the CREATE TABLE. ConstraintSql(q quote.Quoter, name pgxapi.TableName, index int) string // Expresses the constraint as a constructor + literals for the API type. GoString() string }
Constraint represents data that augments the data-definition SQL statements such as CREATE TABLE.
type Constraints ¶
type Constraints []Constraint
Constraints holds constraints.
func (Constraints) FkConstraints ¶
func (cc Constraints) FkConstraints() FkConstraints
FkConstraints returns only the foreign key constraints in the Constraints slice.
type FkConstraint ¶
type FkConstraint struct { ForeignKeyColumn string // only one column is supported Parent Reference Update, Delete Consequence }
FkConstraint holds a pair of references and their update/delete consequences. ForeignKeyColumn is the 'owner' of the constraint.
func FkConstraintOfField ¶
func FkConstraintOfField(field *schema.Field) FkConstraint
FkConstraintOfField constructs a foreign key constraint from a struct field.
func FkConstraintOn ¶
func FkConstraintOn(column string) FkConstraint
FkConstraintOn constructs a foreign key constraint in a fluent style.
func (FkConstraint) ConstraintSql ¶
ConstraintSql constructs the CONSTRAINT clause to be included in the CREATE TABLE.
func (FkConstraint) GoString ¶
func (c FkConstraint) GoString() string
func (FkConstraint) NoCascade ¶
func (c FkConstraint) NoCascade() FkConstraint
NoCascade changes both the Update and Delete consequences to NoAction.
func (FkConstraint) OnDelete ¶
func (c FkConstraint) OnDelete(consequence Consequence) FkConstraint
OnDelete sets the delete consequence.
func (FkConstraint) OnUpdate ¶
func (c FkConstraint) OnUpdate(consequence Consequence) FkConstraint
OnUpdate sets the update consequence.
func (FkConstraint) RefersTo ¶
func (c FkConstraint) RefersTo(tableName string, column string) FkConstraint
RefersTo sets the parent reference. The column may be blank.
func (FkConstraint) RelationshipWith ¶
func (c FkConstraint) RelationshipWith(child pgxapi.TableName) Relationship
RelationshipWith constructs the Relationship that is expressed by the parent reference in the FkConstraint and the child's foreign key.
The table names do not include any prefix.
type FkConstraints ¶
type FkConstraints []FkConstraint
FkConstraints holds foreign key constraints.
func (FkConstraints) Find ¶
func (cc FkConstraints) Find(fkColumn string) (FkConstraint, bool)
Find finds the FkConstraint on a specified column.
type Reference ¶
Reference holds a table + column reference used by constraints. The table name should not include any schema or other prefix. The column may be blank, but the functionality is then reduced (there will be insufficient metadata to use Relationship methods).
type Relationship ¶
type Relationship struct {
Parent, Child Reference
}
Relationship represents a parent-child relationship. Only simple keys are supported (compound keys are not supported).
func (Relationship) IdsUnusedAsForeignKeys ¶
IdsUnusedAsForeignKeys finds all the primary keys in the parent table that have no foreign key in the dependent (child) table. The table tbl provides the database or transaction handle; either the parent or the child table can be used for thi purpose.
func (Relationship) IdsUsedAsForeignKeys ¶
IdsUsedAsForeignKeys finds all the primary keys in the parent table that have at least one foreign key in the dependent (child) table.