clear icon indicating copy to clipboard operation
clear copied to clipboard

Unable to insert objects under a has_many through: "table_name" association

Open jwaldrip opened this issue 4 years ago • 1 comments

Given the following model, my after :save hook is not allowing me to add associated objects that have a has_many through relationship. It fails on compile with the error:

In lib/clear/src/clear/sql/insert_query.cr:147:13

 147 | @values << args
               ^-
Error: no overload matches 'Array(Array(Array(JSON::Any) | Array(PG::BoolArray) | Array(PG::CharArray) | Array(PG::Float32Array) | Array(PG::Float64Array) | Array(PG::Int16Array) | Array(PG::Int32Array) | Array(PG::Int64Array) | Array(PG::NumericArray) | Array(PG::StringArray) | Array(PG::TimeArray) | BigFloat | BigInt | Bool | Char | Clear::Expression::UnsafeSql | Float32 | Float64 | Hash(String, JSON::Any) | Int16 | Int32 | Int64 | Int8 | JSON::Any | PG::Geo::Box | PG::Geo::Circle | PG::Geo::Line | PG::Geo::LineSegment | PG::Geo::Path | PG::Geo::Point | PG::Geo::Polygon | PG::Numeric | Slice(UInt8) | String | Time | UInt16 | UInt32 | UInt64 | UInt8 | Nil))#<<' with type Tuple(Hash(String, UUID))

Overloads are:
 - Array(T)#<<(value : T)
class Shard
  include Clear::Model

  # Columns
  primary_key name: "id", type: :uuid
  column manifest : Manifest::Shard
  column name : String
  column version : SemanticVersion
  column git_tag : SemanticVersion?
  column license : String?
  column description : String?
  column crystal : SemanticVersion?
  column tags : Array(String)?

  # Associations
  belongs_to project : Project, key_type: UUID
  has_many dependencies : ShardDependency, foreign_key: "parent_id", foreign_key_type: UUID
  has_many authors : Author, through: "shard_authors", foreign_key_type: UUID

  # Copy spec data to shard
  before :validate do |model|
    shard = model.as(Shard)
    shard.name = shard.manifest.name
    shard.version = SemanticVersion.parse(shard.manifest.version)
    shard.license = shard.manifest.license
    shard.description = shard.manifest.description
    shard.crystal = (crystal = shard.manifest.crystal) ? SemanticVersion.parse(crystal) : nil
    shard.tags = shard.manifest.tags
  end

  after :save do |model|
    shard = model.as(Shard)
    (shard.manifest.authors || [] of Manifest::Shard::Author).each do |manifest_author|
      author = ::Author.query.find_or_create({ name: manifest_author.name, email: manifest_author.email }){}
      shard.authors << author
    end
  end
end

jwaldrip avatar Apr 21 '20 23:04 jwaldrip

The issue seemed to be with UUID foreign keys. I was able to resolve it by going back to auto-incrementing keys. So there is a bug here, but I am not blocked for the time being.

jwaldrip avatar Apr 25 '20 13:04 jwaldrip