volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

foreign key column names different from x_id #343

Closed saulortega closed 6 years ago

saulortega commented 6 years ago

If my foreign key column is named foo_id, SQLBoiler generates func (o *Bar) Foo(mods ...qm.QueryMod) fooQuery.

But, if the foreign key column is named in any other way different from x_id, the Method and the Struct Field will be both named the same. That's not possible in Go, so it will throw an error.

In that case, I suggets to use the UpSigngular name of the foreign table.

aarondl commented 6 years ago

The rules for relationship naming are spelled out here: https://github.com/volatiletech/sqlboiler/blob/v3/boilingcore/text_helpers.go#L10

But, if the foreign key column is named in any other way different from x_id, the Method and the Struct Field will be both named the same

I'm not sure what you mean unless I see a real example here. The tests are passing and everything seems to be behaving according to those rules which I think are good rules. Without clarification I don't know what your proposal to the rules are. But in general I probably won't want to change them unless it makes a lot of sense. Aliases will rescue you from this situation in case we don't amend the rules.

saulortega commented 6 years ago
// Tpru1Sub is an object representing the database table.
type Tpru1Sub struct {
    Llave     string `boil:"llave" json:"llave" toml:"llave" yaml:"llave"`
    FkeyTpru1 string `boil:"fkey_tpru1" json:"fkey_tpru1" toml:"fkey_tpru1" yaml:"fkey_tpru1"`
    Foo       string `boil:"foo" json:"foo" toml:"foo" yaml:"foo"`

    R *tpru1SubR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L tpru1SubL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

// FkeyTpru1 pointed to by the foreign key.
func (o *Tpru1Sub) FkeyTpru1(mods ...qm.QueryMod) tpru1Query {
    queryMods := []qm.QueryMod{
        qm.Where("llave=?", o.FkeyTpru1),
    }

    queryMods = append(queryMods, mods...)

    query := Tpru1s(queryMods...)
    queries.SetFrom(query.Query, "\"tpru1\"")

    return query
}
Tpru1Sub.FkeyTpru1 // Field
Tpru1Sub.FkeyTpru1() // Method

Fields and Methods can't be named the same.

With aliases I can change the Field, not the Method. For my case of use the more natural is to rename the Method, not the Field.

aarondl commented 6 years ago

You can also change the method using aliases. There's a section in the readme that shows you how to rename relationships. If there's some problem with the documentation or the feature re-open this and we'll figure it out.

The problem here is that the pluralization is failing. Typically this would be named FkeyTprus or something, but because the 1 is there, it ruins the ability for the pluralization library to add an s anywhere. Now you'd still have the same problem if it was a one-to-one relationship since those don't get pluralized. But again - this is what aliases were designed and it should definitely let you rename the method.

saulortega commented 6 years ago

Thank you.

It's working now. :+1: