TJUzengle
TJUzengle

Reputation: 1

Insert a piece of data into mysql by using gorm

I want to insert a piece of data into my database(MySQL). However, I failed.

Error 1364: Field 'envelopeId' doesn't have a default value.

I found that some people say that this is because the primary key is of int type and not set to auto-increment, but this is actually a varchar type.

var envelope Envelope
envelope = Envelope{
    "100000000001",
    1,
    0,
    100,
}
fmt.Println(db.Create(&envelope).Error)

Upvotes: 0

Views: 531

Answers (1)

Dylan Reimerink
Dylan Reimerink

Reputation: 7958

This error means that you are trying to insert an NULL value into a non-nullable field, and since there is also no default value specified it gives you and error. I assume "100000000001" is your primary key, if this is the case, it is set in your struct and you need to check that that field has the gorm:"column:envelopeId" tag set. By convention GORM will use snake case for column names, not camel case like you have in your DB.

Upvotes: 1

Related Questions