-
Notifications
You must be signed in to change notification settings - Fork 47
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
Can not generate struct with relation #14
Comments
When there's an unknown type, the generator tries to import it from the appropriate package. It seems there's an issue when importing the dependency tree when following objectbox import path itself in Could you please specify the Go version and your OS version and which steps did you follow to install ObjectBox? Also, is the project available publicly by any chance? I could try to replicate it the situation. |
I installled it by running The code is not public no. If i know where to look i could potentially find something. I tried looking but the type checking logic was a bit cryptic at first glance, i might need to take another look at it. |
Now that i have a little more understand on how objectbox works, #15, is it intended to have relation between models in different packages ? |
A model defines all entities in a single database, what a "database schema" is called in RDBMS. I have not tested this yet, but you could, in theory, combine entities from multiple Go packages to form a single database (model) by using the //go:generate go run github.com/objectbox/objectbox-go/cmd/objectbox-gogen -persist ./path/to/.../objectbox-model.json Could you try that to see if it helps you create a single model (database) |
Thanks for the info. I moved them both to the same package and the orriginal error presits 🤔 So it seems to not be related that they are different packages. Tried both ways. Both in package A and them both in package B. |
Is it possible that this has to do with the C depedency? @vaind was you abel to reproduce this ? |
This one is a little scary,
Its trying to write to type A struct {
ID uint64
CreatedAt time.Time `objectbox:"type:int64 converter:TimeInt64"`
Name string
ShowID uint64
}
type B struct {
ID uint64
A *A `objectbox:"link"`
QueuedAt time.Time `objectbox:"type:int64 converter:TimeInt64"`
} |
So the reason for the permission denied is, its executing go get in the directory and that tries to write to go.mod. I have a feeling this generator can not work with go mod. I when i make sure that the packages that are missing are located in im my GOPATH, then the generator manages to go to the next dependecy and tell me that its missing from my gopath. |
So after running Im getting the following error now:
but as you can see here, it is Why is there a need of type checking anyway? It is going waay to deep in the depedency tree and its importing stuff that is not even needed to generate the struct. As the struct is basically strings and ints... |
I've added
then ran
in this dir and was able to reproduce the problem @vaind It seems that its not related to linking relations but rather type checking. |
So, my current work around is to generate the code in a clean package that has no imports and copy paste it for now. |
TLDR; Generator runs a Go built-in type-checker when it needs to discover info about unknown types. Having models in a separate package, without unnecessary dependencies is the easiest way to 1. avoid unrelated type-checker errors; 2. keep the generator fast (because it doesn't have to go through the dependencies). If the generator encounters a field with a non-trivial type (e.g. another struct, a named type/type alias, etc), it invokes Go built-in type checker which provides type information. The thing is there are no means to limit the scope on which the type checker runs - we just give it the package and it goes around to any depth it needs (unfortunately). Therefore, when there are errors, the type checker returns those. Recently, I've noticed that even if the type checker returned errors, it tried to continue analyzes even in case of an error so the relevant type we were actually looking for may still be resolved just find. That should help with your situation as well. As for the |
This is indeed what i ended up doing. And it works fine now. I rather have models in the package they belong to. By doing it this way it also means i can use 1 database + ObjectBox object which kind of solves #15 as well 👍 Happy to have helped and thanks for your time 👍 |
You're welcome, in case you have further ideas for improvement, feel free to reach out or create a PR |
When generating the following struct
which has a relation to the show struct, i manage the get the following error:
it leads me to believe that there is something wrong with the object box package? I tried diging into the code but its a little cryptic and could not follow the logic of typing checking.
If i remove the link between the 2, the generator is able to generate without problems. Any ideas ?
The text was updated successfully, but these errors were encountered: