Hello there! I've defined the following schema:
// User holds the schema definition for the User entity.
type User struct {
ent.Schema
}
// Fields of the User.
func (u User) Fields() []ent.Field {
return []ent.Field{
field.
UUID("id", uuid.UUID{}).
Default(uuid.New),
field.
Bool("is_admin").
Default(false),
field.
String("username").
Match(regexp.MustCompile(usernameRegexp)).
Unique(),
field.
String("avatar_url").
Match(regexp.MustCompile(urlRegexp)).
Optional().
Nillable(),
field.
String("email").
Match(regexp.MustCompile(emailRegexp)).
Unique(),
field.
String("encrypted_password").
Sensitive(),
field.
Time("registered_at").
Default(time.Now),
}
}
In code above I expect that as I wrote line(s) field.Time("registered_at").Default(time.Now) value for field registered_at will be equal to current time by default and does not require to be set manually.
Then I'm running go generate ./ent and trying to use generated entity to create user:
// ...
func (svc *userService) Create(ctx context.Context, options dto.UserCreateOptions) (*ent.User, error) {
encryptedPassword, err := bcrypt.GenerateFromPassword([]byte(options.Password), bcrypt.DefaultCost)
if err != nil {
return nil, errors.Wrap(err, "userService.Create.pwdEncrypt error")
}
u, err := svc.client.User.
Create().
SetEmail(options.Email).
SetUsername(options.Username).
SetEncryptedPassword(string(encryptedPassword)).
Save(ctx)
if err != nil {
return nil, errors.Wrap(err, "userService.Create.Save error")
}
return u, nil
}
// ...
In runtime I get following error:
runtime error: invalid memory address or nil pointer dereference goroutine 41 [running]:
github.com/labstack/echo/middleware.RecoverWithConfig.func1.1.1(0x14885d8, 0x1000, 0x0, 0x14f3e00, 0xc0000ae3f0)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/middleware/recover.go:71 +0xf3
panic(0x13f51c0, 0x1710170)
/usr/local/go/src/runtime/panic.go:969 +0x1b9
github.com/tomakado/******/backend/ent.(*UserCreate).defaults(0xc000096370)
$HOME/dev/github.com/tomakado/******/backend/ent/user_create.go:163 +0xbe
github.com/tomakado/******/backend/ent.(*UserCreate).Save(0xc000096370, 0x14ef0e0, 0xc00001c0f0, 0xc0000acc00, 0x3c, 0x3c)
$HOME/dev/github.com/tomakado/******/backend/ent/user_create.go:117 +0x65
github.com/tomakado/******/backend/service.(*userService).Create(0xc0000a4190, 0x14ef0e0, 0xc00001c0f0, 0xc00009c700, 0x10, 0xc00009c710, 0x8, 0xc00009c718, 0x3, 0x203000, ...)
$HOME/dev/github.com/tomakado/******/backend/service/user.go:56 +0x27d
github.com/tomakado/******/backend/controller.UserController.Create(0x1720bb8, 0x14f3e00, 0xc0000ae3f0, 0x155f2508, 0x9a44ee995cb7ab24)
$HOME/dev/github.com/tomakado/******/backend/controller/user.go:38 +0x3ca
github.com/labstack/echo.(*Echo).Add.func1(0x14f3e00, 0xc0000ae3f0, 0x11, 0x0)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/echo.go:490 +0x8f
github.com/labstack/echo/middleware.SecureWithConfig.func1.1(0x14f3e00, 0xc0000ae3f0, 0x100000000, 0xc000121928)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/middleware/secure.go:113 +0x168
github.com/labstack/echo/middleware.RecoverWithConfig.func1.1(0x14f3e00, 0xc0000ae3f0, 0x0, 0x0)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/middleware/recover.go:78 +0x12c
github.com/labstack/echo/middleware.LoggerWithConfig.func2.1(0x14f3e00, 0xc0000ae3f0, 0x0, 0x0)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/middleware/logger.go:118 +0x130
github.com/labstack/echo/middleware.AddTrailingSlashWithConfig.func1.1(0x14f3e00, 0xc0000ae3f0, 0x4, 0xc0000b26a5)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/middleware/slash.go:68 +0x132
github.com/labstack/echo.(*Echo).ServeHTTP(0xc00000c1e0, 0x14ee2e0, 0xc0000e0000, 0xc0000dc000)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/echo.go:593 +0x22f
net/http.serverHandler.ServeHTTP(0xc00021a000, 0x14ee2e0, 0xc0000e0000, 0xc0000dc000)
/usr/local/go/src/net/http/server.go:2843 +0xa3
net/http.(*conn).serve(0xc000094960, 0x14ef0a0, 0xc0000a0400)
/usr/local/go/src/net/http/server.go:1925 +0x8ad
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2969 +0x36c
goroutine 1 [IO wait]:
internal/poll.runtime_pollWait(0x26415b0, 0x72, 0x0)
/usr/local/go/src/runtime/netpoll.go:222 +0x55
internal/poll.(*pollDesc).wait(0xc00009e618, 0x72, 0x0, 0x0, 0x1466378)
/usr/local/go/src/internal/poll/fd_poll_runtime.go:87 +0x45
internal/poll.(*pollDesc).waitRead(...)
/usr/local/go/src/internal/poll/fd_poll_runtime.go:92
internal/poll.(*FD).Accept(0xc00009e600, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/usr/local/go/src/internal/poll/fd_unix.go:394 +0x1fc
net.(*netFD).accept(0xc00009e600, 0x50, 0x18067d0, 0xc000096280)
/usr/local/go/src/net/fd_unix.go:172 +0x45
net.(*TCPListener).accept(0xc0000a28c0, 0xc077cc89af, 0x1723840, 0xc00029fc40)
/usr/local/go/src/net/tcpsock_posix.go:139 +0x32
net.(*TCPListener).AcceptTCP(0xc0000a28c0, 0x100000001, 0xc00029fc70, 0x106861b)
/usr/local/go/src/net/tcpsock.go:248 +0x65
github.com/labstack/echo.tcpKeepAliveListener.Accept(0xc0000a28c0, 0xc00029fc98, 0x10b9e66, 0x5fd7b98e, 0x10418ce)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/echo.go:767 +0x2f
net/http.(*Server).Serve(0xc00021a000, 0x14edfa0, 0xc0000a4150, 0x0, 0x0)
/usr/local/go/src/net/http/server.go:2937 +0x266
github.com/labstack/echo.(*Echo).StartServer(0xc00000c1e0, 0xc00021a000, 0xc00029ff20, 0x2)
$HOME/go/pkg/mod/github.com/labstack/[email protected]+incompatible/echo.go:663 +0x385
github.com/labstac
I believe I've read documentation carefully. Maybe it's because of late night, but I can't figure out what I'm doing wrong.
| Tech | Version |
| ----------- | ------- |
| Go | 1.15.6 darwin/amd64 |
| Ent | 0.5.2 |
| Database | PostgreSQL |
| Driver | github.com/lib/pq |
I'm sorry, here is more relevant stack info

Hey @tomakado and thanks for reporting this issue.
Do you use Hooks or Policy in one of the schemas? If so, I guess that you miss an empty import in your main package (see https://entgo.io/docs/hooks/#schema-hooks).
import _ "<project>/ent/runtime"
Yes, you're right. Sorry and thanks! I'm closing the issue.
馃槂
I'll add tomorrow a better error message for such cases.