toProtoUser casts the automatic generated ID field using type int32 while the DB field type is int64
- Dominant language
- Go
- Stars
- 17.2k
- Forks
- 1k
- PR merge metrics
- No merged PRs in 30d
Description
I'm using postgresql with ent/proto to generate my entities.
once I annotate my schema's entity with entproto.Service() it should automatically generate an ID field, as stated in the docs:
> ent implicitly creates the ID field for the entity, and that field is automatically assigned the number 1.
I've noticed that the ID generated in postgres is of type bigint
>
> Table "public.users"
> Column | Type | Collation | Nullable | Default |
> -------------+--------------------------+-----------+----------+---------------------------
> id | bigint | | not null | generated by default as identity |
and from the generated code I can see that toProtoUser try to cast the id field to int32:
// toProtoUser transforms the ent type to the pb type
> func toProtoUser(e *ent.User) *User {
> v := &User{
> CreateTime: timestamppb.New(e.CreateTime),
> Enabled: e.Enabled,
> FirstName: e.FirstName,
> Id: int32(e.ID),
> LastName: e.LastName,
> Password: e.Password,
> UpdateTime: timestamppb.New(e.UpdateTime),
> Username: e.Username,
The mismatch of the casting causes the returned id of the created object to be different than how it was saved in the DB
- [x] The issue is present in the latest release.
- [x] I have searched the [issues](https://github.com/ent/ent/issues) of this repository and believe that this is not a duplicate.
## Current Behavior 😯
The generated func toProtoUser casts the ID field defined as int64 with int32
That cause the returned ID of the recent created obejct to be different that how it was saved in the DB
## Expected Behavior 🤔
The generated func toProtoUser should return a valid ID field represent the ID of the created object.
## Steps to Reproduce 🕹
Steps:
1. Use a postgresql DB
2. Follow the instructions in https://entgo.io/docs/grpc-generating-proto
3. https://entgo.io/docs/grpc-generating-a-service
4. https://entgo.io/docs/grpc-server-and-client
## Your Environment 🌎
| Tech | Version |
| ----------- | ------- |
| Go | 1.17.1 |
| Ent | 0.10.0 |
| Database | Postgresql |
| Driver | |
Contributor guide
Assessment
This issue has not been assessed yet.