title: Database Inheritance in Go
body: <p>I have a situation where I have a base type, let's call it the <code>Pet</code> type (not the real type but an apt example), and 6 underlying variants of this type that all could have their own individual fields.  Let's say they are each different types of pets, like <code>Dog</code>, <code>Cat</code>, <code>Bird</code>, etc.  Right now they are all organized in the same table called <code>pets</code>, but I think I would benefit from breaking each type out into their own table(<code>dogs</code>, <code>cats</code>, <code>birds</code>, etc) and then inheriting the fields from the main <code>pets</code> table.  To be clear, each of my child tables have unique fields that don't apply to any of the other subtypes.</p>
  
  <p>I'm using <code>database/sql</code>, and I query for data like so:</p>
  
  <pre><code>var getPetsQuery = fmt.Sprintf(`SELECT %v FROM pets`)
  
  func (tx *Tx) GetPets() (pets []*model.Pet, err error) {
      return tx.petQueryRow(getPetsQuery)
  }
  
  func (tx *Tx) petQueryRow(query string, args ...interface{}) (pets []*model.Pet, err error) {
      rows, err := tx.Query(query, args...)
      if err != nil {
          return nil, errors.Stacktrace(PetQueryFailedErr(err))
      }
      defer rows.Close()
  
      for rows.Next() {
          pet := &amp;model.Pet{}
          err = rows.Scan(
              &amp;pet.Id,
              &amp;pet.Uid,
              &amp;pet.Created,
              &amp;pet.Type,
              &amp;pet.LegCount,
          )
          results = append(results, pet)
      }
  
      err = rows.Err()
      if err != nil {
          return nil, errors.Stacktrace(PetQueryFailedErr(err))
      }
      return results, err
  }
  </code></pre>
  
  <p>My question is, using this system, if I wanted to query for a list of pets of all types, how would I go about loading the extra data from each individual subtype table in a single query?  Or would I need to load a list of pets first and then query for the additional data individually?</p>