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

Fixing query with has_one #263

Merged
merged 5 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/query_associations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ end
class NamedSpaced::Organization < BaseModel
table do
has_many locations : Location
has_one president : Staff
end
end

Expand All @@ -20,6 +21,13 @@ class NamedSpaced::Location < BaseModel
end
end

class NamedSpaced::Staff < BaseModel
table do
column name : String
belongs_to organization : Organization
end
end

describe "Query associations" do
it "can query associations" do
post_with_matching_comment = PostBox.create
Expand Down Expand Up @@ -137,5 +145,8 @@ describe "Query associations" do
orgs = NamedSpaced::Organization::BaseQuery.new
.where_locations(NamedSpaced::Location::BaseQuery.new.name("Home"))
orgs.to_sql[0].should contain "INNER JOIN"

staff = NamedSpaced::Staff::BaseQuery.new
staff.to_sql[0].should contain "named_spaced_staffs"
end
end
22 changes: 22 additions & 0 deletions spec/query_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,28 @@ describe Avram::Query do
end
end

context "queries joining with has_one" do
describe "when you query from the belongs_to side" do
it "returns a record" do
line_item = LineItemBox.create &.name("Thing 1")
price = PriceBox.create &.in_cents(100).line_item_id(line_item.id)

query = PriceQuery.new.where_line_items(LineItemQuery.new.name("Thing 1"))
query.first.should eq price
end
end

describe "when you query from the has_one side" do
it "returns a record" do
line_item = LineItemBox.create &.name("Thing 1")
price = PriceBox.create &.in_cents(100).line_item_id(line_item.id)

query = LineItemQuery.new.where_price(PriceQuery.new.in_cents(100))
query.first.should eq line_item
end
end
end

describe "#to_prepared_sql" do
it "returns the full SQL with args combined" do
query = Post::BaseQuery.new.title("The Short Post")
Expand Down
2 changes: 1 addition & 1 deletion src/avram/associations/has_one.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Avram::Associations::HasOne
{% end %}

{% unless foreign_key %}
{% foreign_key = "#{@type.name.underscore}_id".id %}
{% foreign_key = "#{@type.name.underscore.split("::").last.id}_id".id %}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was fixed under the has_many a long time ago, so this now fixes it for the has_one

{% end %}

{% foreign_key = foreign_key.id %}
Expand Down
9 changes: 9 additions & 0 deletions src/avram/base_query_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ class Avram::BaseQueryTemplate
foreign_key: primary_key_name
)
)
{% elsif assoc[:relationship_type] == :has_one %}
join(
Avram::Join::{{ join_type.id }}.new(
from: @@table_name,
to: {{ assoc[:type] }}::TABLE_NAME,
foreign_key: :{{ assoc[:foreign_key] }},
primary_key: primary_key_name
)
)
{% elsif assoc[:through] %}
{{ join_type.downcase.id }}_join_{{ assoc[:through].id }}
__yield_where_{{ assoc[:through].id }} do |join_query|
Expand Down