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

Call model got Error : does not compile: cannot convert db (type *sql.DB) to type models.ModelName #249

Closed aloha1003 closed 6 years ago

aloha1003 commented 6 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.6.0

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

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

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

My Problem code is here

I got models , but I call model in app.go , I got below error message does not compile: cannot convert db (type *sql.DB) to type models.BonusMissio

Dexus commented 6 years ago

import please the mysql driver

https://github.com/go-sql-driver/mysql look at the usage...

aarondl commented 6 years ago

So, there's three problems here:

  1. Dexus is right in that you would have to import the mysql driver for sure before this would work.
  2. You've ellided part of the error message, typically if Go cannot convert a type to an interface, it'll say the reason why it couldn't do that.
  3. In the sample code it looks like you're using the revel web framework and you're inside a request, please ensure that you don't call sql.Open on each request, as each call to that opens a pool of connections (not just one), so it's much more efficient to simply call sql.Open one time and store the db on a struct somewhere and re-use it.
aloha1003 commented 6 years ago

Thx , I fix it .