Skip to content

invalid sql type (interface) in field Table_Name for mysql #18

Description

@FelipeAlafy

Describe the bug
While using gorm auto migration function won't create a table with a EncryptedValue from gorm-crypto

To Reproduce
Create a new struct with gorm.Model and a field with a name and this field has the type of gormcrypto.EncryptedValue

gorm.go

import (
	"crypto/rsa"
	"fmt"
	"io/ioutil"
	"os"

	"github.com/FelipeAlafy/Flex/handler"
	"github.com/jinzhu/gorm"
	gormcrypto "github.com/pkasila/gorm-crypto"
	"github.com/pkasila/gorm-crypto/algorithms"
	"github.com/pkasila/gorm-crypto/helpers"
	"github.com/pkasila/gorm-crypto/serialization"
	_ "gorm.io/driver/mysql"
)

var privateKey *rsa.PrivateKey
var publicKey *rsa.PublicKey

func Connect() *gorm.DB {
	if _, err := os.Stat("private_key.pem"); os.IsNotExist(err) {
		// There is no PEM
		
		// Generate key pair
		privateKey, publicKey, err = helpers.RSAGenerateKeyPair(4096)
	  
		if err != nil {
		  panic(err)
		}
	  
		// Store it
		privateKeyBytes := helpers.RSAPrivateKeyToBytes(privateKey)
		err := ioutil.WriteFile("private_key.pem", privateKeyBytes, 0644)
	  
		if err != nil {
		  panic(err)
		}
	  } else {
		// There is a PEM
		
		// Read PEM file with private key
		bytes, err := ioutil.ReadFile("private_key.pem")
		if err != nil {
		  panic(err)
		}
	  
		// Bytes to private key
		privateKey, err = helpers.RSABytesToPrivateKey(bytes)
		if err != nil {
		  panic(err)
		}
		publicKey = &privateKey.PublicKey
	  }
	  
	  gormcrypto.Init(algorithms.NewRSA(privateKey, publicKey), serialization.NewJSON())

	
	url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local", USER, PASS, HOST, PORT, DB)
	db, err := gorm.Open("mysql", url)
	handler.Error("database/gorm.go >> db", err)
	return db
}
Hello.go
package database

import (
	"github.com/jinzhu/gorm"
	gormcrypto "github.com/pkasila/gorm-crypto"
)

type Hello struct {
	gorm.Model
	Text gormcrypto.EncryptedValue
}

func (h Hello) Save(db *gorm.DB) {
	db.Save(&h)
}

func GetAllHellos(db *gorm.DB) []Hello {
	hellos := []Hello{}
	db.Find(&hellos)
	return hellos
}

Expected behavior
Create a table on mariadb

Screenshots
image

System Information:

  • OS: [Fedora 38]
  • Go version: [go1.20.4 linux/amd64]

Additional context
Using Mariadb

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions