简体   繁体   English

Golang:Gorm对非Gorm迁移表使用Find(&model)

[英]Golang: gorm use Find(&model) for non gorm migrate table

There is table customer_account ( postgres ) which one was migrate from YII2. 有一个表customer_accountpostgres ),该表是从YII2迁移过来的。

DDL: DDL:

CREATE TABLE public.test_table (
  id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('test_table_id_seq'::regclass),
  data JSONB
);

In go project i try to get value from this table. 在go项目中,我尝试从此表中获取价值。

type TableGo struct {
    Id int
    Data string `gorm:"type:jsonb"`
}

    table := TableGo{}
    db.Where("id = ?", 75).Find(&table)
    println(table.Data)

But there is (pq: relation "table_gos" does not exist) 但是有(pq: relation "table_gos" does not exist)

How i can link structure which table without db.AutoMigrate(&TableGo{}) ? 我如何在没有db.AutoMigrate(&TableGo{})情况下链接结构哪个表?

Found the solution: 找到了解决方案:

func(TableGo) TableName() string {
    return "account_status"
}

I think table name in your migration script is wrong. 我认为您的迁移脚本中的表名是错误的。 Because it is not in GORM convention. 因为它不在GORM约定中。 If you want to use that name,you can use following method in your model for custom table name. 如果要使用该名称,则可以在模型中使用以下方法自定义表名称。

func (m *Model) TableName() string {
    return "custom_table_name"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM