blob encode uuids if stored as binary#173
Draft
sbaildon wants to merge 1 commit intoelixir-sqlite:mainfrom
Draft
blob encode uuids if stored as binary#173sbaildon wants to merge 1 commit intoelixir-sqlite:mainfrom
sbaildon wants to merge 1 commit intoelixir-sqlite:mainfrom
Conversation
Member
|
Would you be able to provide a stripped down example of an app where this is occurring so I can fiddle with it? |
Contributor
Author
|
Of course # main.exs
Mix.install([
{:typeid_elixir, "~> 1.1"},
{:ecto, "~> 3.13"},
{:ecto_sql, "~> 3.13"},
{:ecto_sqlite3, "~> 0.21.0"}
])
Enum.each(
[
uuid_type: :binary,
journal_mode: :wal,
],
fn {k, v} -> Application.put_env(:ecto_sqlite3, k, v) end
)
Application.put_env(:myapp, Repo,
migration_primary_key: [name: :id, type: :uuid, null: false],
database: ":memory:",
pool_size: 1
)
Application.put_env(:typeid_elixir, :default_type, :uuid)
defmodule Migration0 do
use Ecto.Migration
def change do
create table("entities", options: "STRICT") do
add(:field, :string)
end
end
end
defmodule Repo do
use Ecto.Repo,
otp_app: :myapp,
adapter: Ecto.Adapters.SQLite3
end
defmodule Entity do
use Ecto.Schema
@primary_key {:id, TypeID, prefix: "entity", autogenerate: true}
schema "entities" do
field(:field, :string)
end
end
defmodule Main do
import Ecto.Query, warn: false
def main do
Repo.__adapter__().storage_down(Repo.config())
:ok = Repo.__adapter__().storage_up(Repo.config())
{:ok, _} = Repo.start_link([])
Ecto.Migrator.run(Repo, [{0, Migration0}], :up, all: true, log_migrations_sql: :info)
Repo.insert!(%Entity{
field: "Post 1"
})
Repo.all(from(e in Entity))
|> dbg()
end
end
Main.main()$ elixir main.exs
17:30:31.238 [info] == Running 0 Migration0.change/0 forward
17:30:31.240 [info] create table entities
17:30:31.241 [info] QUERY OK db=0.1ms
CREATE TABLE "entities" ("id" BLOB NOT NULL PRIMARY KEY, "field" TEXT) STRICT []
17:30:31.241 [info] == Migrated 0 in 0.0s
17:30:31.260 [debug] QUERY ERROR source="entities" db=0.0ms idle=6.4ms
INSERT INTO "entities" ("field","id") VALUES (?1,?2) ["Post 1", #TypeID<"entity_01jz96kc2nfzaanentmz3tsybr">]
** (Exqlite.Error) cannot store TEXT value in BLOB column entities.id
INSERT INTO "entities" ("field","id") VALUES (?1,?2)
(ecto_sql 3.13.2) lib/ecto/adapters/sql.ex:1098: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto 3.13.2) lib/ecto/repo/schema.ex:1000: Ecto.Repo.Schema.apply/4
(ecto 3.13.2) lib/ecto/repo/schema.ex:500: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
(ecto 3.13.2) lib/ecto/repo/schema.ex:381: Ecto.Repo.Schema.insert!/4
main.exs:59: Main.main/0
main.exs:68: (file) |
Contributor
Author
|
If this line from |
Member
|
Thank you @sbaildon I'll take a look at this soon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Marking as a draft because I actually don't know if this is the correct solution
I'm using https://github.com/sloanelybutsurely/typeid-elixir which implements an
Ecto.ParameterizedTypethat can be either a:stringor:uuidtype. As strings they look like "user_01jz8mgbxkevwagbhr8cdqhpqh"I want to use
ecto_sqlite3withuuid_type: :binaryfor efficiency reasonsStoring the TypeID as
:uuidwill drop the prefix, leaving only the suffix "01jz8mgbxkevwagbhr8cdqhpqh", then convert that suffix from base32 to binary uuidv7 — but Ecto blows up inSTRICTtables withBLOBcolumns becausetypeid_elixiris unawareexqliteneeds to receive{:blob, value}, not onlyvalue(binary uuidv7).I've added blob encoding to the dumper pipeline, but again, maybe this is not the correct solution.