mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
dialect/sql/entsql: allow non incremental primary keys (#1074)
* Allow non incremental PKs * Format code with gofmt * Update entc/integration/config/config_test.go Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com> * Update entc/integration/config/config_test.go Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com> * Update entc/integration/config/config_test.go Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com> * Update entc/integration/config/config_test.go Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com> * Sort imports Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
This commit is contained in:
@@ -50,6 +50,17 @@ type Annotation struct {
|
||||
// }
|
||||
//
|
||||
Size int64 `json:"size,omitempty"`
|
||||
|
||||
// Incremental defines the autoincremental behavior of a column. For example:
|
||||
//
|
||||
// incrementalEnabled := true
|
||||
// entsql.Annotation{
|
||||
// Incremental: &incrementalEnabled,
|
||||
// }
|
||||
//
|
||||
// By default, this value is nil defaulting to whatever best fits each scenario.
|
||||
//
|
||||
Incremental *bool `json:"incremental,omitempty"`
|
||||
}
|
||||
|
||||
// Name describes the annotation name.
|
||||
@@ -85,6 +96,9 @@ func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
|
||||
if s := ant.Size; s != 0 {
|
||||
a.Size = s
|
||||
}
|
||||
if s := ant.Incremental; s != nil {
|
||||
a.Incremental = s
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ func NewTable(name string) *Table {
|
||||
|
||||
// AddPrimary adds a new primary key to the table.
|
||||
func (t *Table) AddPrimary(c *Column) *Table {
|
||||
c.Key = PrimaryKey
|
||||
t.AddColumn(c)
|
||||
t.PrimaryKey = append(t.PrimaryKey, c)
|
||||
return t
|
||||
|
||||
@@ -129,7 +129,7 @@ func (*SQLite) cType(c *Column) (t string) {
|
||||
func (d *SQLite) addColumn(c *Column) *sql.ColumnBuilder {
|
||||
b := sql.Column(c.Name).Type(d.cType(c)).Attr(c.Attr)
|
||||
c.unique(b)
|
||||
if c.Increment {
|
||||
if c.PrimaryKey() && c.Increment {
|
||||
b.Attr("PRIMARY KEY AUTOINCREMENT")
|
||||
}
|
||||
c.nullable(b)
|
||||
|
||||
Reference in New Issue
Block a user