andrenth / sequoia

OCaml type-safe query builder with syntax tree extension
123 stars 16 forks source link

Support for string foreign keys #5

Open donut opened 7 years ago

donut commented 7 years ago

I have a table with a primary key that is of type VARCHAR and is referenced by another table, but it looks like Sequoia only supports. At least, when I try to compile I get this:

Error: This expression has type
         string Alias.Field.t = (Alias.Field.table, string) Sequoia_field.t
       but an expression was expected of type
         (Alias.Field.table, int) Sequoia_mysql.M.Field.t =
           (Alias.Field.table, int) Sequoia_field.t
       Type string is not compatible with type int

Example:

open Sequoia_mysql

module URL = struct
  include (val table "url")
  let id = Field.int "id"
  (* ... *)
end

module Alias = struct
  include (val table "alias")
  let name = Field.string "name"
  let url = Field.foreign_key "url" ~references:URL.id
  (* ... *)
end

module Use = struct
  include (val table "use")
  let id = Field.int "id"
  let alias = Field.foreign_key "alias" ~references:Alias.name (* <- error *)
  let url = Field.foreign_key "url" ~references:URL.id
  (* ... *)
end
andrenth commented 7 years ago

This is sort of by design, as I believe non-integer foreign keys are not considered a good practice. I could be convinced otherwise, but one of the goals of Sequoia is to try to enforce good practices.

donut commented 7 years ago

While I'm not 100% convinced in general, I have been convinced for my project that non-integer foreign keys are not the best idea. Thanks