/
errors.go
40 lines (37 loc) · 1.1 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package database
// http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html
const (
ERROR_CODE_INVALID_TEXT_REPRESENTATION = "22P02"
ERROR_CODE_UNIQUE_VIOLATION = "23505"
ERROR_CODE_FOREIGN_KEY_CONSTRAINTS_VIOLATION = "23503"
ERROR_CODE_NUMERIC_FIELD_OVERFLOW = "22003"
)
// Returns the default unique constraint name generated by psql
// when there is no name given
// http://stackoverflow.com/questions/4107915/postgresql-default-constraint-names
func DefaultUniqueConstraintName(tableName string, columnNames ...string) string {
count := 0
size := len(columnNames)
columnName := ""
for _, column := range columnNames {
columnName += column
if count != size-1 {
columnName += "_"
}
count++
}
return tableName + "_" + columnName + "_key"
}
func DefaultForeignKeyConstraintName(tableName string, columnNames ...string) string {
count := 0
size := len(columnNames)
columnName := ""
for _, column := range columnNames {
columnName += column
if count != size-1 {
columnName += "_"
}
count++
}
return tableName + "_" + columnName + "_fkey"
}