Skip to content

Commit

Permalink
add SetNotNull separate from primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
pbnjay committed Aug 31, 2013
1 parent b7d1729 commit d9421a6
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions gorp.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ type ColumnMap struct {
gotype reflect.Type
isPK bool
isAutoIncr bool
isNotNull bool
}

// Rename allows you to specify the column name in the table
Expand All @@ -511,6 +512,13 @@ func (c *ColumnMap) SetUnique(b bool) *ColumnMap {
return c
}

// SetNotNull adds "not null" to the create table statements for this
// column, if nn is true.
func (c *ColumnMap) SetNotNull(nn bool) *ColumnMap {
c.isNotNull = nn
return c
}

// SetMaxSize specifies the max length of values of this column. This is
// passed to the dialect.ToSqlType() function, which can use the value
// to alter the generated type for "create table" statements
Expand Down Expand Up @@ -681,11 +689,11 @@ func (m *DbMap) createTables(ifNotExists bool) error {
stype := m.Dialect.ToSqlType(col.gotype, col.MaxSize, col.isAutoIncr)
s.WriteString(fmt.Sprintf("%s %s", m.Dialect.QuoteField(col.ColumnName), stype))

if col.isPK {
if col.isPK || col.isNotNull {
s.WriteString(" not null")
if len(table.keys) == 1 {
s.WriteString(" primary key")
}
}
if col.isPK && len(table.keys) == 1 {
s.WriteString(" primary key")
}
if col.Unique {
s.WriteString(" unique")
Expand Down

0 comments on commit d9421a6

Please sign in to comment.