简体   繁体   English

golang gin gorm插入并设置primary_key但primary_key为空

[英]golang gin gorm insert and set primary_key but primary_key got null

I use gin gorm mysql build application.我使用 gin gorm mysql 构建应用程序。

I set topic_id primary_key auto_increment not null in model.go as follow:我在 model.go 中设置 topic_id primary_key auto_increment not null 如下:

type Topic struct {
    gorm.Model
    TopicId    uint64 `gorm:"PRIMARY_KEY;AUTO_INCREMENT;NOT NULL"`
    TopicName  string
    TopicDesc  string
    OwnerId    int
    CreateIP   string
    CreateTime uint64
    UpdateTime uint64
}

create topic in service.go在 service.go 中创建主题

type TopicCreateService struct{
    TopicName string `form:"topic_name" json:"topic_name" binding:"required,min=1,max=30"`
    TopicDesc string `form:"topic_desc" json:"topic_desc" binding:"required,min=1,max=300"`
    OwnerId int `form:"owner_id" json:"owner_id" binding:"required,min=1,max=30"`
}

func (service *TopicCreateService) Create(c *gin.Context) serializer.Response{
    topic := model.Topic{
        TopicName:service.TopicName,
        TopicDesc:service.TopicDesc,
        OwnerId:service.OwnerId,
        CreateIP:c.ClientIP(),
        CreateTime:uint64(time.Now().UnixNano()),
        UpdateTime:0,
    }

    if err:=model.DB.Create(&topic).Error;err!=nil{
        return serializer.ParamErr("创建话题失败", err)
    }
    return serializer.BuildTopicResponse(topic)
}

在此处输入图片说明

I want topic_id be my primary_key and not null auto-increment.我希望 topic_id 是我的 primary_key 而不是 null 自动增量。 What`s wrong?怎么了?

you've included gorm.Model in your struct.你已经在你的结构中包含了gorm.Model This means that your model migration/database will give an error:这意味着您的模型迁移/数据库会报错:

Error 1075: Incorrect table definition; there can be only one auto column and it must be defined as a key

If you remove the gorm.Model from your Topic struct, you will be good.如果你从你的Topic结构中移除gorm.Model ,你会很好。

package model

import (
    `github.com/jinzhu/gorm`
)

type WithoutModel struct {
    MyId int64 `gorm:"primary_key;auto_increment;not_null"`
    Name string
}

func ModelSave(tx *gorm.DB) {
    wo := WithoutModel{Name:"Without the model"}
    tx.Save(&wo)
}

After running ModelSave a couple of times, I have:运行ModelSave几次后,我有:

MariaDB [gmodel]> select * from without_models;
+-------+-------------------+
| my_id | name              |
+-------+-------------------+
|     1 | Without the model |
|     2 | Without the model |
+-------+-------------------+
2 rows in set (0.000 sec)
gorm.Model 
// gorm.Model 定义
type Model struct {
  ID        uint `gorm:"primary_key"`
  CreatedAt time.Time
  UpdatedAt time.Time
  DeletedAt *time.Time
}

gorm conventions戈姆公约

暂无
暂无

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

相关问题 插入 UUID 主键索引的性能 - Inserting performance for UUID primary_key index SQL Alchemy:表 primary_key - 意外的关键字参数错误 - SQL Alchemy : Table primary_key - unexpected keyword argument error 如何使用Python获取数据库中最后插入的记录的primary_key - How to get primary_key of last inserted record in database with Python 如何在数据库级别获取primary_key的列名? - How to get column name for primary_key at database level? 是否有可能使mysql表接受引用其他表的primary_key列的空值? - Is it possible to have a mysql table accept a null value for a primary_key column referencing a different table? Mysql从(1,2 ,, ...)中的primary_key非常慢的表中选择* - Mysql select * from tables where primary_key in (1,2,,…) very slow 如何计算或获取primary_key不在外部表中的行记录? - how to count or get record of rows, whose primary_key is not in foreign table? 字段 'index_id' 没有默认值”在尝试使用 primary_key 作为 Hibernate 中的复合键的一部分时 - Field 'index_id' doesn't have a default value" when trying to use the primary_key as part of composite key in Hibernate MySQL查询返回,Schema_Name,Table_NAme,Count,Size_of_Table,Primary_Key和Auto_Increment_Key - MySQL Query To Return, Schema_Name, Table_NAme, Count, Size_of_Table, Primary_Key and Auto_Increment_Key Split a (slow) mysql query in chunks of many primary_key ranges (id 1-100, id 101-200, ...) - Split a (slow) mysql query in chunks of many primary_key ranges (id 1-100, id 101-200, ...)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM