martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
424 stars 24 forks source link

Make reference fields work when targetting non-int primary key fields #101

Closed ellmetha closed 1 year ago

ellmetha commented 1 year ago

Description

Presently, the many_to_one and one_to_one fields only work when the targetted model has an integer primary key field.

Let's ensure that it is possible to target any kind of model, regardless of its primary key field. For example, it should be possible to define many_to_one and one_to_one fields targeting models with UUID primary keys:

class Article < Marten::Model
  field :id, :uuid, primary_key: true
  field :title, :string, max_size: 255
  field :body, :text

  after_initialize :initialize_id

  private def initialize_id
    @id ||= UUID.random
  end
end

class Comment < Marten::Model
  field :id, :int, primary_key: true, auto: true
  field :article, :many_to_one, to: Article
  field :text, :text
end