Skip to content

Compile error using .has_one with views #1152

Description

@akadusei

I have just started to use views. I have the following:

class ProjectData < BaseModel
  alias PrimaryKeyType = Project::PrimaryKeyType # <= Without this I get the compile error

  belongs_to project : Project

  view :projects_data do
    # ...
  end
  # ...
end
class Project < BaseModel
  # ...
  has_one data : ProjectData # <= This triggers the compile error

  table :projects do
    # ...
  end
  # ...
end

The compile error:

There was a problem expanding macro 'define_has_one_base_query'

Code in macro 'has_one'

 26 | define_has_one_base_query(Project, data, ProjectData, project_id, nil)
      ^
Called macro defined in lib/avram/src/avram/associations/has_one.cr:80:11

 80 | private macro define_has_one_base_query(class_type, assoc_name, model, foreign_key, through)

Which expanded to:

 > 69 | add_preload do |records|
 > 70 |   ids = records.map(&.id)
 > 71 |   empty_results = {} of ProjectData::PrimaryKeyType => Array(ProjectData)
                                ^--------------------------
Error: undefined constant ProjectData::PrimaryKeyType

The culprit is here:

empty_results = {} of {{ model }}::PrimaryKeyType => Array({{ model }})

It seems .has_many and .belongs_to macros calls would suffer the same error if the association is a view (regular views, at least).

Is this expected to happen? If so, should this be documented?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions