pop icon indicating copy to clipboard operation
pop copied to clipboard

Support column exclusions on inner associations

Open kiambogo opened this issue 6 years ago • 0 comments

Description

Currently there does not seem to be a way for a model with a nested association to define column exclusions for its association. Take for example the following:

type User struct {
  name string `db:"name"`
  status string `db:"status"`
  pet pet `has_many: "pets"`
}

type Pet struct {
  name string `db:"name"`
  type string `db:"type"`
}

and given the following tables in Postgres:

postgres=# \d users
                 Table "public.users"
 Column | Type | Collation | Nullable |    Default
--------+------+-----------+----------+----------------
 name   | text |           |          |
 status | text |           | not null | 'active'::text

postgres=# \d pets
                Table "public.pets"
 Column | Type | Collation | Nullable |   Default
--------+------+-----------+----------+-------------
 name   | text |           |          |
 type   | text |           | not null | 'dog'::text

We are able to create a new User and persist it, while letting the db default the status by leveraging the excludeColumns parameter on the Save method:

u := User {
  name: "kiambogo",
  pet: Pet {
    name: "chester"
  }
}

db.Eager().Save(u, "status")

However, this will not work correctly since the association insert will fail since the SQL will include the type column but with a NULL value (violates the not-null constraint). Ideally, there is a simple way to tell a column to use the db default (either through db tags, a new type (maybe leverage nulls?), or allow for specifying columns to exclude in the association models somehow.

Steps to Reproduce the Problem

See above.

Expected Behavior

I'd expect there to be a way to allow associated models to leverage db defaults.

Actual Behavior

There should be a way for associated models to leverage db defaults.

Info

  • Mac OSX
  • Pop v4.9.4
  • Using Pop through Buffalo

kiambogo avatar Feb 06 '19 20:02 kiambogo