go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.19k stars 3.88k forks source link

Foreign Key AutoMigrate default value #5222

Open alicewenli opened 2 years ago

alicewenli commented 2 years ago

Your Question

Is there a way to prevent foreign keys from copying over the default value of its reference?

In the docs, it says:

For a has one relationship, a foreign key field must also exist, the owner will save the primary key of the model belongs to it into this field. The field’s name is usually generated with has one model’s type plus its primary key, for the above example it is UserID.

I'm using a Postgres database with primary key default values of generated by default as identity. However, if the primary key is used as a foreign key, then the foreign key field will also have a default value of generated by default as identity. I don't think this is useful and also results in an extra sequence being created.

Example:

type User struct {
    ID        int64 `gorm:"type:bigint generated by default as identity"`
    Profiles  []Profile
}
type Profile struct {
    ID        int64 `gorm:"type:bigint generated by default as identity"`
    UserID    int64
    User      User
}

The above schema results in tables like:

                                       Table "public.users"
   Column   |           Type           | Collation | Nullable |             Default              
------------+--------------------------+-----------+----------+----------------------------------
 id         | bigint                   |           | not null | generated by default as identity
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)

                                      Table "public.profiles"
   Column   |           Type           | Collation | Nullable |             Default              
------------+--------------------------+-----------+----------+----------------------------------
 id         | bigint                   |           | not null | generated by default as identity
 user_id    | bigint                   |           | not null | generated by default as identity
Indexes:
    "profiles_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "fk_users_profiles" FOREIGN KEY (user_id) REFERENCES users(id)

                  List of relations
 Schema |          Name           |   Type   |  Owner  
--------+-------------------------+----------+---------
 public | users                   | table    | ---
 public | users_id_seq            | sequence | ---
 public | profiles                | table    | ---
 public | profiles_id_seq         | sequence | ---
 public | profiles_user_id_seq    | sequence | ---

The profiles_user_id_seq is unnecessary, and so is the default value for the user_id column in the profiles table.

The document you expected this should be explained

https://gorm.io/docs/has_one.html#Override-Foreign-Key

Expected answer

gurkan0791 commented 2 years ago

same problem

a631807682 commented 2 years ago

You can use autoincrement, gorm will convert it to a serial which is old implementation in postgres. Maybe we should support it, but it doesn't belong to the tag type

ALTER [ COLUMN ] column_name ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]

https://www.postgresql.org/docs/current/sql-altertable.html