Giter Club home page Giter Club logo

Comments (6)

Gurpartap avatar Gurpartap commented on May 18, 2024

You'll need to implement Scan and Value on your JSON type to provide to and from transformers.

from dat.

Gurpartap avatar Gurpartap commented on May 18, 2024

If you can adapt to using []byte instead, this implementation by @ansel1 is handy: https://github.com/jinzhu/gorm/issues/516#issuecomment-109055198

from dat.

pyrossh avatar pyrossh commented on May 18, 2024

@Gurpartap Thanks. @atrniv solved this problem for me the same way. I forgot this issue since we moved away from dat.

type JSONO map[string]interface{}
type JSONA []interface{}

func (jo JSONO) String() string {
    data, err := json.Marshal(jo)
    if err != nil {
        panic(err.Error())
    }
    return string(data)
}

func (jo JSONO) Value() (driver.Value, error) {
    return json.Marshal(jo)
}

func (jo JSONO) Scan(data []byte) error {
    return json.Unmarshal(data, jo)
}

from dat.

heynemann avatar heynemann commented on May 18, 2024

Using this approach I'm getting (JSONMap is same as JSONO):

<*errors.errorString | 0xc420165810>: {
          s: "sql: converting Exec argument #2's type: unsupported type map[interface {}]interface {}, a map",
      }

My model is:

type Game struct {
	ID        string       `db:"id"`
	Name      string       `db:"name"`
	Metadata  JSONMap      `db:"metadata"`
	CreatedAt dat.NullTime `db:"created_at"`
	UpdatedAt dat.NullTime `db:"updated_at"`
}

The dat code is:

                        //Metadata defaults to {}
			game := models.Game{
				Name: uuid.NewV4().String(),
			}
			err := db.
				InsertInto("games").
				Record(&game).
				Returning("id", "created_at", "updated_at").
				QueryStruct(&game)

Any ideas?

from dat.

mgutz avatar mgutz commented on May 18, 2024

What is metadata postgres type, jsonb? I would recommend dat.JSON for jsonb.

I'm not familiar with JSONO type? Please provide gist of DDL for game and type definition of JSONMap.

from dat.

heynemann avatar heynemann commented on May 18, 2024

Sorry man. Was actually a problem with fixtures. I just found out, like a minute ago, lol. Thanks for the fast response. Awesome lib.

from dat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.