create returns nil for custom primary keys

Open
#389 4 comments 0 reactions 1 assignee View on GitHub

@solnic is already working on this.

Since Dec 30, 2020.

Assessment

This issue has not been assessed yet.

Description

bug

Describe the bug

When using a randomly generated string as a primary key, create returns nil.

[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

To Reproduce

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

Expected behavior

create should return the record that was created.

My environment

  • Affects my production application: NO
  • Ruby version: 2.7.2
  • OS: macOS 10.15.7
Dominant language
Ruby
Stars
220
Forks
97
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rom-rb/rom-sql

All issues in rom-rb/rom-sql

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.