volatiletech / sqlboiler

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

related.IDKotum undefined #72

Closed bxcodec closed 7 years ago

bxcodec commented 7 years ago

I got this error when i'm test the models. Fyi, i'm using mysql

related.IDKotum undefined

This happens only for table that use 2 same foreign keys

for example sample.sql

use Sample;

CREATE TABLE `SampleMain` (
  `id_kota` int(11) NOT NULL,
  `sequence_number` int(11) DEFAULT NULL,
  `name` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id_kota`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `Detail` (
  `id_harga_detail` int(11) NOT NULL,
  `source_id` int(11) DEFAULT NULL,
  `dest_id` int(11) DEFAULT NULL,
  `harga` double DEFAULT NULL,
  `create_at` datetime DEFAULT NULL,
  `update_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id_harga_detail`),
  KEY `fk_list_asal_idx` (`source_id`),
  KEY `fk_list_tujuan_idx` (`dest_id`),
  CONSTRAINT `fk_detail_asal` FOREIGN KEY (`source_id`) REFERENCES `SampleMain` (`id_kota`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_detail_tujuan` FOREIGN KEY (`dest_id`) REFERENCES `SampleMain` (`id_kota`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
bxcodec commented 7 years ago

After the models generated, i got the error when i test the models.

*ps, could you create topic on stackoverflow.com please ?? My reputation not enough to create new topic/tag.

**ps : sorry for my bad grammar, and bad name's column that using a little my local language.

aarondl commented 7 years ago

Yes, the problem here is that "kota" according to our pluralization library is the plural of "kotum". So it tries to singularize your column name in one area and that breaks it.

We will most likely fix this by tomorrow and release a minor version update to deal with it. For now you can apply this patch:

diff --git a/strmangle/inflect.go b/strmangle/inflect.go
index 78945b6..8499bda 100644
--- a/strmangle/inflect.go
+++ b/strmangle/inflect.go
@@ -184,5 +184,6 @@ func newBoilRuleset() *inflect.Ruleset {
        rs.AddIrregular("move", "moves")
        rs.AddIrregular("zombie", "zombies")
        rs.AddIrregular("cookie", "cookies")
+       rs.AddUncountable("kota")
        return rs
 }

One reason I'm asking you to try this patch, is to see if you can find any other problems before we release this patch. Better to fix many problems at once :)

bxcodec commented 7 years ago

Well, thanks for fast response. Maybe i need to try more. And of course , i need "more documentation" :)

How to deal with null.*Datatypes ? I don't see on your documentation , any explanation about this.

for example this is the model:

`type OkAkun struct {
    IDAkun      int         `boil:"id_akun" json:"id_akun" toml:"id_akun" yaml:"id_akun"`
    Username    null.String `boil:"username" json:"username,omitempty" toml:"username" yaml:"username,omitempty"`
    PhoneNumber null.String `boil:"phone_number" json:"phone_number,omitempty" toml:"phone_number" yaml:"phone_number,omitempty"`
    Email       null.String `boil:"email" json:"email,omitempty" toml:"email" yaml:"email,omitempty"`
    CreateAt    null.Time   `boil:"create_at" json:"create_at,omitempty" toml:"create_at" yaml:"create_at,omitempty"`
    UpdateAt    null.Time   `boil:"update_at" json:"update_at,omitempty" toml:"update_at" yaml:"update_at,omitempty"`
    Password    null.String `boil:"password" json:"password,omitempty" toml:"password" yaml:"password,omitempty"`

    R *OkAkunR `boil:"-" json:"-" toml:"-" yaml:"-"`
    L OkAkunL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}` 

And when i try to insert,

i got error's like this, (this happen on all null.*Datatypes ) cannot use "bxcodec@gmail.com" (type string) as type null.String in field value

aarondl commented 7 years ago

Can you show the line that's causing this? Also check out the documentation for the null package we use: https://godoc.org/gopkg.in/nullbio/null.v6

This shouldn't happen unless there's some problem with the struct generation. Can you show me the "show create table" from mysql for that table? Alternatively show the relevant part of the output when you run sqlboiler --debug mysql

bxcodec commented 7 years ago

This is the Sql :

CREATE TABLE `OkAkun` (
  `id_akun` int(11) NOT NULL AUTO_INCREMENT COMMENT '   ',
  `username` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `phone_number` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `email` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  `create_at` datetime DEFAULT NULL,
  `update_at` datetime DEFAULT NULL,
  `password` varchar(45) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id_akun`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

this is how i tried to insert data :

a := &models.OkAkun{
        Username : "bxcodec",
        PhoneNumber: "882292348292",
        Email : "bxcodec@gmail.com",
    }
if err := a.Insert(db); err != nil {
        log.Error("Failed TO Insert Akun", "err", err);

    }
aarondl commented 7 years ago

I also asked for the line of code that is causing the error, that is essential in order to figure out what's going on :)

bxcodec commented 7 years ago

I've update my comment

aarondl commented 7 years ago

Yes, you have to use the null package helpers. I suggest null.StringFrom: https://godoc.org/gopkg.in/nullbio/null.v6#StringFrom

a := &models.OkAkun{
  Username:    null.StringFrom("bxcodec"),
  PhoneNumber: null.StringFrom("882292348292"),
  Email:       null.StringFrom("bxcodec@gmail.com"),
}
if err := a.Insert(db); err != nil {
  log.Error("Failed TO Insert Akun", "err", err);
}

But the bigger problem is that you should NOT have these as nullable in your database. That's a very bad database design, please change your table so that these fields cannot be null.

bxcodec commented 7 years ago

Yeahh... I just figured it after i do show create table . But, look for the positive, now i can deal with the null-able column :D

Thanks for the response, and I hope, this is also documented as soon as the development process.