2021-08-25 15:34:33 +02:00
package schema
import (
"fmt"
)
const (
InvalidRelation = iota
HasOneRelation
BelongsToRelation
HasManyRelation
ManyToManyRelation
)
type Relation struct {
2024-11-08 14:51:23 +01:00
// Base and Join can be explained with this query:
//
// SELECT * FROM base_table JOIN join_table
Type int
Field * Field
JoinTable * Table
BasePKs [ ] * Field
JoinPKs [ ] * Field
OnUpdate string
OnDelete string
Condition [ ] string
2021-08-25 15:34:33 +02:00
PolymorphicField * Field
PolymorphicValue string
2024-11-08 14:51:23 +01:00
M2MTable * Table
M2MBasePKs [ ] * Field
M2MJoinPKs [ ] * Field
2021-08-25 15:34:33 +02:00
}
2024-01-15 14:08:07 +01:00
// References returns true if the table to which the Relation belongs needs to declare a foreign key constraint to create the relation.
// For other relations, the constraint is created in either the referencing table (1:N, 'has-many' relations) or a mapping table (N:N, 'm2m' relations).
func ( r * Relation ) References ( ) bool {
return r . Type == HasOneRelation || r . Type == BelongsToRelation
}
2021-08-25 15:34:33 +02:00
func ( r * Relation ) String ( ) string {
return fmt . Sprintf ( "relation=%s" , r . Field . GoName )
}