-
Notifications
You must be signed in to change notification settings - Fork 962
override id column #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Currently the "id" field is implied so there's no way to override the underlying storage key. The above setting should give a better error message where a reserved field name is used. Are you looking to rename the "id" column name, provide your own PKs or both? |
This is part of our roadmap and it should be addressed in the upcoming week (or 2). |
@alexsn im not looking to rename "id". I'm looking to keep it "id" but change its type from integer to string, and also be able to provide my own value when i create a row. Thanks ✌️ |
Seems to work with the latest matcher branch, but there isn't a uuid type available. Is there any way to use a Postgres UUID primary key with ent? |
Hey @BitPhinix, this feature is still in development. |
@a8m Feature Proposal I'm currently using type generated by ent, and I want to make json reponse with no ID field.
I thought I could solve this by doing something like this. But didn't worked well. |
Hey @aca, did you try to override the struct tag? See - https://entgo.io/docs/schema-fields/#struct-tags |
@a8m I meant ID field which is generated by entc not by defining ent.Field
Defining "id" field doesn't seems to work. Really sorry If I misunderstood. |
There's no option to override the ID's struct-tag except using custom template (which is too complex in this case imo). This definitely should be addressed. I'll update the issue once we get to it. |
Hey, dear @a8m , I am rewriting a project with go , I enjoy using ent, but this project has a table with a |
I ran the codegen with this option in the schema and it worked for me. func (User) Fields() []ent.Field {
return []ent.Field{
field.Int("id").
StorageKey("user_id"),
// more fields..
}
} The database in MySQL was created as follows: You are welcome to try it. Like I mentioned above, once we get to this task, we'll handle all these cases. |
I know the issue is still being worked on, but just as an update: Given the following The following error shows up from the create table operation:
The interesting part, is that if I change the schema to say: Then the same error happens. I noticed that the If I manually change |
@a8m another issue I discovered (just in case you missed it), is that // IDEQ applies the EQ predicate on the ID field.
func IDEQ(id string) predicate.Spec {
return predicate.Spec(func(s *sql.Selector) {
id, _ := strconv.Atoi(id)
s.Where(sql.EQ(s.C(FieldID), id))
},
)
} This should just be // IDEQ applies the EQ predicate on the ID field.
func IDEQ(id string) predicate.Spec {
return predicate.Spec(func(s *sql.Selector) {
s.Where(sql.EQ(s.C(FieldID), id))
},
)
} But in what scenario do you expect an ID that is a string but must be converted to an integer? Will removing that one line completely not be sufficient? Thanks! |
Hey @marwan-at-work and thanks for reporting these issues. Regarding your comments:
I'm aware about this and it should be addressed. I'm pretty sure this is the last thing left for closing this issue.
Where did you encounter this code? in your generated code or in this repository? If you don't run codegen with This (legacy) option was added to allow using the same generate I don't see any reason for using this option, except our exceptional case that was used for migrating from Neptune to MySQL. I'll suggest to not use it because we plan to drop it soon. |
I'm using the latest commit of I created a reproduction repository here: https://github.com/marwan-at-work/entrepro You can see that my schema has the following ID definition https://github.com/marwan-at-work/entrepro/blob/master/ent/schema/spec.go#L14:L22 But then then the code that was generated from If that can just be removed from the template, I'm happy to contribute a fix, here's what I did locally and it seemed to work: marwan-at-work@e3b004b#diff-a01877e4cc82393fb3d3f48188992b02 Thanks! |
Thanks for helping debugging this @marwan-at-work. Can you please run the codegen as describe here, or just use:
Just to make sure we're running the same version (as in go.mod). I'll update once it's fixed. |
@marwan-at-work - I've added a fix for this, and checked it on your repo using this version: Please check it and update me if there's any issue with this. |
@a8m just tried it with that commit and it looks great!! |
All comments that were raised in this issue addressed (hopefully), although there are some docs changes that need to be done. Please give it a try and update us if there's any issue with this. Closing. |
If we have multi schema in same dic. I only want one schema replace |
I think we could support some options to define a different primary key for schema, for example: |
You can. See the second option here: https://entgo.io/docs/schema-fields#id-field |
This works as you mentioned, but just for your information, generating migration diff for the following scenario is not as expected:
Actual Migration File: I confirmed that |
Is there a way to redeclare the id column as a custom string type?
If I put the following in my schema:
field.String("id").Unique()
The generated code ends up broken because it creates two declarations of "id", but furthermore it ignores the fact that it's a string, and declares it as an integer instead.
This might be related to the UUID discussions, but it would be great if we can define our own custom primary keys as strings that we provide at runtime.
The text was updated successfully, but these errors were encountered: