Skip to content
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

problem when table has FK to itself #39

Closed
ryanbarry opened this issue Sep 20, 2016 · 5 comments
Closed

problem when table has FK to itself #39

ryanbarry opened this issue Sep 20, 2016 · 5 comments

Comments

@ryanbarry
Copy link

i've got the following table (postgres):

create table if not exists posts (
       id serial primary key not null,
       uuid uuid not null default (uuid_generate_v4()) unique,
       created_at_utc timestamp without time zone not null default (now() at time zone 'utc'),
       approved boolean not null default false,
       flagged boolean not null default false,
       -- problem here:
       parent_uuid uuid references posts (uuid),
       --
       updated_at_utc timestamp without time zone default null
);

but the generated code gives the following errors when building:

models/posts.go:330: cannot call non-function p.ParentUUID (type null.String)
models/posts.go:334: type Post has both field and method named ParentUUID

am i doing something wrong? is there a way to name things that won't cause this problem? Thanks in advance! Other than this minor issue, I'm really enjoying using this!

@aarondl
Copy link
Member

aarondl commented Sep 20, 2016

Hi there @ryanbarry

The problem here is that we treat _id suffixes specially. When we generate a foreign key "lookup" helper, we typically take the foreign key name and drop _id off of it. It's a convention noted in the
pro tips section of the readme. However I guess it sort of becomes a requirement when the foreign key is on the same table :(

We could consider _uuid to the list of things that we drop, but then maybe someone else will come along with another prefix. Anyway, changing your column name to _id should fix the issue. And we'll see what we do about this in the long term.

Thanks for using sqlboiler!

@nullbio
Copy link
Member

nullbio commented Sep 20, 2016

Hi @ryanbarry, is there a particular reason you are not using your unique uuid as your primary key, opposed to the serial id, or vice versa?

@nullbio
Copy link
Member

nullbio commented Sep 20, 2016

@ryanbarry we've added uuid to the trim suffixes in the dev branch. Once these are merged into master (hopefully today) then you should not need to change any code, as long as you update your sqlboiler and regenerate.

@nullbio nullbio closed this as completed Sep 20, 2016
@ryanbarry
Copy link
Author

this is awesome, i really appreciate the quick turnaround! reason for having serial id & unique uuid is to expose uuid as an external identifier while keeping the serial id as internal to our datastore, while keeping querying simple. thanks again 😃

@nullbio
Copy link
Member

nullbio commented Sep 21, 2016

Hey @ryanbarry, 2.0.1 has been released which includes the fixes for the issues you were having. Thanks.

aarondl added a commit that referenced this issue Nov 9, 2016
- There was no thought put in if a relationship struct already existed
  when it went to be used a second time so it was being clobbered.
- Fix #39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants