Skip to content

has_one doesn't allow custom foreign_key #740

Open
@FinnIckler

Description

I'm trying to implement a 1:1 relationship between two tables without duplicating data in a new column. The issue is that has_one does not allow specifying a custom foreign_key, while belongs_to does.
This means a new column is created which contains a set of ids, even though I only want a 1:1 relationship.
The only solution I came up with is to use belongs_to on both ends. Is this intended?

Example:

# models/registration
class Registration
  include Dynamoid::Document
  table name: EnvConfig.DYNAMO_REGISTRATIONS_TABLE, capacity_mode: nil, key: :attendee_id
  # does not work
  has_one :history, class: RegistrationHistory, foreign_key: :attendee_id
  # works, but is semantically incorrect
  belongs_to :history, class: RegistrationHistory, foreign_key: :attendee_id
end

# models/registration_history
class RegistrationHistory
  include Dynamoid::Document

  table name: EnvConfig.REGISTRATION_HISTORY_DYNAMO_TABLE, capacity_mode: nil, key: :attendee_id
  # works
  belongs_to :registration, foreign_key: :attendee_id
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions