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

SQLBoiler fails on creating schema during `go test` when there's a non-FK constraint #749

Open ejulen opened 4 years ago

ejulen commented 4 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)?

4.1.2

What is your database and version (eg. Postgresql 10)

MariaDB 10.5.3

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

CREATE TABLE bar (
    id INTEGER PRIMARY KEY
);

CREATE TABLE foo (
    id INTEGER PRIMARY KEY,
    details text NOT NULL DEFAULT "{}",
    bar_id INTEGER NOT NULL,
    FOREIGN KEY(bar_id) REFERENCES bar(id),
    CHECK (JSON_VALID(details))
);

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

I ran go test ./models after generating them and got the following error:

ERROR 1064 (42000) at line 63: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CONSTRAINT `CONSTRAINT_1` CHECK (json_valid(`details`))

Looks like this line could be responsible. When manipulating the output of mysqldump that pattern combined with fKeyDestroyer breaks the SQL (excerpt):

DROP TABLE IF EXISTS `foo`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `foo` (
  `id` int(11) NOT NULL,
  `details` text NOT NULL DEFAULT '{}',
  `bar_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `bar_id` (`bar_id`)  CONSTRAINT `CONSTRAINT_1` CHECK (json_valid(`details`))
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

(note the CONSTRAINT line)

aarondl commented 4 years ago

Yeah. This is definitely the case. I'd accept a PR to fix this, but I'm hardly interested in maintaining these tests to fit everyone's database. They were a nice idea, but unfortunately it depends too much on the shape of everyone's databases - much moreso than just using sqlboiler itself.