I need to generate random strings (a UUID in this example) as primary keys:
require ‘sqlite3’
require ‘rom’
require ‘rom-sql’
require ‘rom-repository’
require ‘pry’
rom = ROM.container(:sql, 'sqlite::memory', logger: Logger.new(STDOUT)) do |config|
config.default.create_table :notes do
column :id, :string, primary_key: true
column :text, :string
end
config.relation(:notes) do
schema(:notes) do
attribute :id, ROM::Types::String.default { SecureRandom.uuid }
attribute :text, ROM::Types::String
primary_key :id
end
end
end
class NotesRepo < ROM::Repository[:notes]
commands :create
end
repo = NotesRepo.new(rom)
binding.pry
puts 'done'
In this example, I can successfully create records with random ids, but after the INSERT, an incorrect SELECT statement is made, so nil is returned rather than the newly created record:
[1] pry(main)> repo.create(text: 'blah')
I, [2020-12-09T10:48:53.147651 #14228] INFO -- : (0.000085s) INSERT INTO `notes` (`id`, `text`) VALUES ('129778db-7cdb-4509-9484-86a92403d13a', 'blah')
I, [2020-12-09T10:48:53.147987 #14228] INFO -- : (0.000065s) SELECT `id`, `text` FROM `notes` WHERE (NULL IN (1))
=> nil
[2] pry(main)> repo.create(text: 'blahblahblah')
I, [2020-12-09T10:48:59.883589 #14228] INFO -- : (0.000061s) INSERT INTO `notes` (`id`, `text`) VALUES ('a89e11cc-c7d2-46c2-8eb2-ead99044b8b4', 'blahblahblah')
I, [2020-12-09T10:48:59.883807 #14228] INFO -- : (0.000058s) SELECT `id`, `text` FROM `notes` WHERE (NULL IN (2))
=> nil
[3] pry(main)> rom.relations[:notes].count
I, [2020-12-09T10:49:03.836183 #14228] INFO -- : (0.000092s) SELECT count(*) AS 'count' FROM `notes` LIMIT 1
=> 2