diff --git a/model.go b/model.go index 5af17bf..ed0be4e 100644 --- a/model.go +++ b/model.go @@ -387,7 +387,7 @@ func (m *Model) Search(w http.ResponseWriter, r *http.Request) { childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ Domain: domainName, Request: r, - User: m.valueLookup("user", w, r), + User: m.valueLookup(types.FieldUser, w, r), ModuleName: m.naming.ModuleName, TableName: m.naming.TableName, Scenario: types.ScenarioList, @@ -490,7 +490,7 @@ func (m *Model) Create(w http.ResponseWriter, r *http.Request) { diffAttrs = make([]*types.DiffAttr, 0, 10) childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ Domain: domainName, - User: m.valueLookup("user", w, r), + User: m.valueLookup(types.FieldUser, w, r), Request: r, ModuleName: m.naming.ModuleName, TableName: m.naming.TableName, @@ -588,7 +588,7 @@ func (m *Model) Update(w http.ResponseWriter, r *http.Request) { childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ Domain: domainName, Request: r, - User: m.valueLookup("user", w, r), + User: m.valueLookup(types.FieldUser, w, r), ModuleName: m.naming.ModuleName, TableName: m.naming.TableName, Scenario: types.ScenarioUpdate, @@ -671,7 +671,7 @@ func (m *Model) Delete(w http.ResponseWriter, r *http.Request) { } childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ Domain: m.valueLookup(types.FieldDomain, w, r), - User: m.valueLookup("user", w, r), + User: m.valueLookup(types.FieldUser, w, r), Request: r, ModuleName: m.naming.ModuleName, TableName: m.naming.TableName, @@ -790,7 +790,7 @@ func (m *Model) Export(w http.ResponseWriter, r *http.Request) { childCtx := context.WithValue(r.Context(), RuntimeScopeKey, &types.RuntimeScope{ Domain: domainName, Request: r, - User: m.valueLookup("user", w, r), + User: m.valueLookup(types.FieldUser, w, r), ModuleName: m.naming.ModuleName, TableName: m.naming.TableName, Scenario: types.ScenarioExport, @@ -988,7 +988,7 @@ __end: if result.TotalCount > result.SuccessCount { result.FailureFile = failureFile } - result.Duration = time.Now().Sub(tm) + result.Duration = time.Since(tm) m.getHook().afterImport(ctx, result) } @@ -1022,7 +1022,7 @@ func (m *Model) Import(w http.ResponseWriter, r *http.Request) { //这里用background的context childCtx := context.WithValue(context.Background(), RuntimeScopeKey, &types.RuntimeScope{ Domain: domainName, - User: m.valueLookup("user", w, r), + User: m.valueLookup(types.FieldUser, w, r), ModuleName: m.naming.ModuleName, TableName: m.naming.TableName, Scenario: types.ScenarioImport, @@ -1075,7 +1075,7 @@ func (m *Model) Import(w http.ResponseWriter, r *http.Request) { fast, _ = strconv.ParseBool(qs.Get("__fast")) go m.importInternal(childCtx, domainName, schemas, filename, fast, extraFields) m.response.Success(w, types.ImportResponse{ - UID: m.valueLookup("user", w, r), + UID: m.valueLookup(types.FieldUser, w, r), Status: "committed", }) } @@ -1096,7 +1096,7 @@ func newModel(v any, db *gorm.DB, naming types.Naming) *Model { Clauses: map[string]clause.Clause{}, } if err := model.statement.Parse(v); err == nil { - if model.statement.Schema.PrimaryFieldDBNames != nil && len(model.statement.Schema.PrimaryFieldDBNames) > 0 { + if model.statement != nil && model.statement.Schema != nil && len(model.statement.Schema.PrimaryFieldDBNames) > 0 { model.primaryKey = model.statement.Schema.PrimaryFieldDBNames[0] } } diff --git a/types/types.go b/types/types.go index 605cfdc..a2bf95e 100644 --- a/types/types.go +++ b/types/types.go @@ -93,6 +93,7 @@ const ( ) const ( + FieldUser = "user" FieldDomain = "domain" )