Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jwaldrip opened this issue Apr 21, 2020 · 1 comment

Comments

@jwaldrip
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant