1

I am trying to create a rest API. I have already a database. I want to create a struct linked to a database table. But when I run the project beego automatically creates an "id" primary key for the model registered. How to avoid this in beego?

My code example:

Model:
type Person struct {
    PersonId string `json:"person_id"`
    name string `json:"name"`
    email string `json:"email"`
}

Problem: Encounter need a primary key field when using beego It creates an id field in db table with default null value.

Note: person_id is the primary key in person table.

1 Answers1

1

If you want Beego’s ORM to have a different primary key, you should use this:


type Person struct {
    PersonId int64 `orm:"pk" json:"person_id"`
    name string `json:"name"`
    email string `json:"email"`
}

You can check the official documentation here in the primary key section: Beego Model Definition

This might be happening because when creating your table you have primary_key set to auto. Which is the default Beego behaviour.

Please check this article also: https://developpaper.com/question/beegos-orm-gives-the-primary-key-value-every-update-delete-read/