volatiletech / sqlboiler

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

Word `areas` will not convert to singular `area`. Generates name collision. #168

Closed jfernstad closed 7 years ago

jfernstad commented 7 years ago

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

SQLBoiler v2.4.0

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

Using MySQL.

CREATE TABLE IF NOT EXISTS `areas` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(128) NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Further information. What did you do, what did you expect?

Hi there, so the issue seems to be the table name areas. It will generate the struct Areas and also the global function Areas, resulting in a compilation error:

$ go build
# webservice/models
models/areas.go:836: Areas redeclared in this block
    previous declaration at models/areas.go:25

If I change the name to cars, everything works. I verified this by adding areas to the TestSingular test for strmangle and it failed, see diff below. Adding areas to TestPlural works as expected, the issue seems to be to convert areas to area. The diff below also contain a fix, but it might not be the general fix (why this is an issue and not a pull request). It just adds areas and area as specific singulars in strmangle/inflect.go.

diff --git a/strmangle/inflect.go b/strmangle/inflect.go
index 6238c39..c9ca8e4 100644
--- a/strmangle/inflect.go
+++ b/strmangle/inflect.go
@@ -100,6 +100,7 @@ func newBoilRuleset() *inflect.Ruleset {
        rs.AddSingular("ta", "tum")
        rs.AddSingular("ia", "ium")
        rs.AddSingular("analyses", "analysis")
+       rs.AddSingular("areas", "area")
        rs.AddSingular("bases", "basis")
        rs.AddSingular("diagnoses", "diagnosis")
        rs.AddSingular("parentheses", "parenthesis")
diff --git a/strmangle/strmangle_test.go b/strmangle/strmangle_test.go
index b07cee6..66bf582 100644
--- a/strmangle/strmangle_test.go
+++ b/strmangle/strmangle_test.go
@@ -139,6 +139,7 @@ func TestSingular(t *testing.T) {
                {"hello_people", "hello_person"},
                {"hello_person", "hello_person"},
                {"friends", "friend"},
+               {"areas", "area"},
                {"hello_there_people", "hello_there_person"},
        } 

Unclear how this affect other words but it's a problem for us.

Thanks

aarondl commented 7 years ago

This is in fact probably how we would do this. The inflect library doesn't always catch everything, hence all the exceptions that already exist. I'd accept this as a PR if you'd like to make one :)

jfernstad commented 7 years ago

Ok, here's the pull request. šŸ˜„ šŸ‘