entc/gen: skip checking required fields with db-based default values (#3204)

This commit is contained in:
Ariel Mashraki
2023-01-02 22:59:27 +02:00
committed by GitHub
parent 41bf915604
commit 542f36d4ab
12 changed files with 298 additions and 9 deletions

View File

@@ -8,8 +8,10 @@ package entv2
import (
"context"
"errors"
"fmt"
"entgo.io/ent/dialect"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/migrate/entv2/blog"
"entgo.io/ent/entc/integration/migrate/entv2/user"
@@ -23,6 +25,12 @@ type BlogCreate struct {
hooks []Hook
}
// SetOid sets the "oid" field.
func (bc *BlogCreate) SetOid(i int) *BlogCreate {
bc.mutation.SetOid(i)
return bc
}
// SetID sets the "id" field.
func (bc *BlogCreate) SetID(i int) *BlogCreate {
bc.mutation.SetID(i)
@@ -78,6 +86,12 @@ func (bc *BlogCreate) ExecX(ctx context.Context) {
// check runs all checks and user-defined validators on the builder.
func (bc *BlogCreate) check() error {
switch bc.driver.Dialect() {
case dialect.MySQL, dialect.SQLite:
if _, ok := bc.mutation.Oid(); !ok {
return &ValidationError{Name: "oid", err: errors.New(`entv2: missing required field "Blog.oid"`)}
}
}
return nil
}
@@ -116,6 +130,10 @@ func (bc *BlogCreate) createSpec() (*Blog, *sqlgraph.CreateSpec) {
_node.ID = id
_spec.ID.Value = id
}
if value, ok := bc.mutation.Oid(); ok {
_spec.SetField(blog.FieldOid, field.TypeInt, value)
_node.Oid = value
}
if nodes := bc.mutation.AdminsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,