ariga / entimport

A tool for generating Ent schema from SQL schema
Apache License 2.0
174 stars 49 forks source link

not enough arguments in call to field.UUID #39

Open kaykhan opened 2 years ago

kaykhan commented 2 years ago

I'm having an issue generating the schema from my existing postgres database. Ive been followign this post - https://entgo.io/blog/2021/10/11/generating-ent-schemas-from-existing-sql-databases/

When i execgo generate ./ent i get the following error:

entc/load: load schema dir: /home/kay/powerlevel/backend/ent/schema/authentication.go:19:276: not enough arguments in call to field.UUID have (string) want (string, driver.Valuer) exit status 1 ent/generate.go:3: running "go": exit status 1

database


-- CreateTable
CREATE TABLE "users" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "email" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),
    "deleted_at" TIMESTAMP(3),

    CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "authentication" (
    "id" UUID NOT NULL DEFAULT gen_random_uuid(),
    "type" TEXT NOT NULL,
    "service_id" TEXT NOT NULL,
    "session_id" TEXT NOT NULL,
    "password" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),
    "deleted_at" TIMESTAMP(3),
    "user_id" UUID NOT NULL,

    CONSTRAINT "authentication_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "authentication" ADD CONSTRAINT "authentication_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

I exec the following to get the code generated - go run ariga.io/entimport/cmd/entimport -dsn "postgres://root:password@localhost:5432/powerlevel?sslmode=disable"

ent/schema/authentication.go

// Code generated by entimport, DO NOT EDIT.

package schema

import (
    "entgo.io/ent"
    "entgo.io/ent/dialect/entsql"
    "entgo.io/ent/schema"
    "entgo.io/ent/schema/edge"
    "entgo.io/ent/schema/field"
    "github.com/google/uuid"
)

type Authentication struct {
    ent.Schema
}

func (Authentication) Fields() []ent.Field {
    return []ent.Field{field.UUID("id", uuid.UUID{}), field.String("type"), field.String("service_id"), field.String("session_id"), field.String("password"), field.Time("created_at"), field.Time("updated_at").Optional(), field.Time("deleted_at").Optional(), field.UUID("user_id").Optional(uuid.UUID{})}
}
func (Authentication) Edges() []ent.Edge {
    return []ent.Edge{edge.From("user", User.Type).Ref("authentications").Unique().Field("user_id")}
}
func (Authentication) Annotations() []schema.Annotation {
    return []schema.Annotation{entsql.Annotation{Table: "authentication"}}
}

ent/schema/user.go

// Code generated by entimport, DO NOT EDIT.

package schema

import (
    "entgo.io/ent"
    "entgo.io/ent/schema"
    "entgo.io/ent/schema/edge"
    "entgo.io/ent/schema/field"
    "github.com/google/uuid"
)

type User struct {
    ent.Schema
}

func (User) Fields() []ent.Field {
    return []ent.Field{field.UUID("id", uuid.UUID{}), field.String("email"), field.Time("created_at"), field.Time("updated_at").Optional(), field.Time("deleted_at").Optional()}
}
func (User) Edges() []ent.Edge {
    return []ent.Edge{edge.To("authentications", Authentication.Type)}
}
func (User) Annotations() []schema.Annotation {
    return nil
}

This is my first time using entgo, am i doing something wrong?

go version go1.18.4 linux/amd64
ariga.io/entimport v0.0.0-20220722070026-e5b57d96ab7c // indirect
entgo.io/ent v0.11.1 // indirect

Just playing around with it, it seems like in ent/schema/authentication.go this field.UUID("user_id").Optional(uuid.UUID{}) should have been field.UUID("user_id", uuid.UUID{}).Optional()? After editing that change and then attempting to generate, it does generate without errors. But it seems like i should not be editing these generated files. Although i dont think this manual change has worked properly to setup the edges. as the following statement brings back no authentication data despite it being the db. users, err := services.EntClient.User.Query().WithAuthentications().All(services.EntContext)