Giter Club home page Giter Club logo

Comments (14)

ericmj avatar ericmj commented on August 24, 2024

Please include the full error message. Which oid is failing? We've removed automatic text fallback for types we don't handle by default which is probably causing this error.

from postgrex.

jasonwebster avatar jasonwebster commented on August 24, 2024

I'm seeing:

** (ArgumentError) no extension found for oid `114`
  (postgrex) lib/postgrex/types.ex:285: Postgrex.Types.fetch!/2
  (postgrex) lib/postgrex/types.ex:218: Postgrex.Types.format/2
  (postgrex) lib/postgrex/protocol.ex:279: anonymous fn/2 in Postgrex.Protocol.encode_params/1
  (elixir) lib/enum.ex:977: anonymous fn/3 in Enum.map/2
  (elixir) lib/enum.ex:1261: Enum."-reduce/3-lists^foldl/2-0-"/3
  (elixir) lib/enum.ex:977: Enum.map/2
  (postgrex) lib/postgrex/protocol.ex:275: Postgrex.Protocol.encode_params/1
  (postgrex) lib/postgrex/protocol.ex:245: Postgrex.Protocol.send_params/2

after attempting an insert on a table that has a json column in it.

from postgrex.

ericmj avatar ericmj commented on August 24, 2024

@jasonwebster oid 114 is json. See the readme for an example json extension.

from postgrex.

zipme avatar zipme commented on August 24, 2024

I have a table called 'subjects' which has a jsonb column, bellow is the full error message:

05:21:16.182 [debug] SELECT s0."id", s0."data" FROM "subjects" AS s0 LIMIT 1 [] (61.8ms)
** 
(ArgumentError) no extension found for oid `3802`
    (postgrex) lib/postgrex/types.ex:285: Postgrex.Types.fetch!/2
    (postgrex) lib/postgrex/types.ex:218: Postgrex.Types.format/2
    (elixir) lib/enum.ex:977: Enum."-map/2-lc$^0/1-0-"/2
    (elixir) lib/enum.ex:977: Enum."-map/2-lc$^0/1-0-"/2
    (postgrex) lib/postgrex/protocol.ex:124: Postgrex.Protocol.message/3
    (postgrex) lib/postgrex/connection.ex:428: Postgrex.Connection.new_data/2
    (postgrex) lib/postgrex/connection.ex:302: Postgrex.Connection.handle_info/2
    (stdlib) gen_server.erl:599: :gen_server.handle_msg/5

from postgrex.

ericmj avatar ericmj commented on August 24, 2024

You fix it the same way. Just change type: "json" to type: "jsonb".

from postgrex.

zipme avatar zipme commented on August 24, 2024

THANKS! It's working now!

from postgrex.

cmelgarejo avatar cmelgarejo commented on August 24, 2024

@zipme how do you parse the jsonb format? throw me a bone here :) 🍖

from postgrex.

zipme avatar zipme commented on August 24, 2024

@cmelgarejo I use the example code from README:

defmodule Extensions.JSON do
  alias Postgrex.TypeInfo

  @behaviour Postgrex.Extension
  @json ["json", "jsonb"]

  def init(_parameters, opts),
    do: Keyword.fetch!(opts, :library)

  def matching(_library),
    do: [type: "json", type: "jsonb"]

  def format(_library),
    do: :binary

  def encode(%TypeInfo{type: type}, map, _state, library) when type in @json,
    do: library.encode!(map)

  def decode(%TypeInfo{type: type}, json, _state, library) when type in @json,
    do: library.decode!(json)
end

from postgrex.

cmelgarejo avatar cmelgarejo commented on August 24, 2024

Yeah, I did too, but when you select a table with jsonb data, the poison library fails to parse the binary data, that's what I meant :(

-----Mensaje original-----
De: "zipme" [email protected]
Enviado el: ‎13/‎03/‎2015 23:31
Para: "ericmj/postgrex" [email protected]
CC: "Christian Melgarejo Bresanovich" [email protected]
Asunto: Re: postgrex no extension found for oid (#60)

@cmelgarejo I use the example code in README:
defmodule Extensions.JSON do
alias Postgrex.TypeInfo

@behaviour Postgrex.Extension
@JSON ["json", "jsonb"]

def init(_parameters, opts),
do: Keyword.fetch!(opts, :library)

def matching(_library),
do: [type: "json", type: "jsonb"]

def format(_library),
do: :binary

def encode(%TypeInfo{type: type}, map, _state, library) when type in @JSON,
do: library.encode!(map)

def decode(%TypeInfo{type: type}, json, _state, library) when type in @JSON,
do: library.decode!(json)
end


Reply to this email directly or view it on GitHub.

from postgrex.

zipme avatar zipme commented on August 24, 2024

@cmelgarejo

Try this:

defmodule JSONB do
  alias Postgrex.TypeInfo

  @behaviour Postgrex.Extension

  def init(_parameters, opts),
    do: Keyword.fetch!(opts, :library)

  def matching(_library),
    do: [type: "jsonb"]

  def format(_library),
    do: :text

  def encode(%TypeInfo{type: "jsonb"}, map, _state, library),
    do: library.encode(map)

  def decode(%TypeInfo{type: "jsonb"}, json, _state, library),
    do: library.decode(to_string(json), keys: :atoms)

end

Where I use Poison to decode/encode json data

from postgrex.

cmelgarejo avatar cmelgarejo commented on August 24, 2024

@zipme thanks! worked like a charm! jsonb needed to be decoded as TEXT instead of binary kind of beats the concept 😕

from postgrex.

ericmj avatar ericmj commented on August 24, 2024

You should always use the binary format whenever you can because then postgrex can encode/decodes higher types of it (such as arrays and composite types).

binary format jsonb sends a single byte with the version before the json so you can just pattern match that away and then call your json library.

defmodule Extensions.JSON do
  alias Postgrex.TypeInfo

  @behaviour Postgrex.Extension
  @json ["json", "jsonb"]

  def init(_parameters, opts),
    do: Keyword.fetch!(opts, :library)

  def matching(_library),
    do: [type: "json", type: "jsonb"]

  def format(_library),
    do: :binary

  def encode(%TypeInfo{type: "json"}, map, _state, library),
    do: library.encode!(map)
  def encode(%TypeInfo{type: "jsonb"}, map, _state, library),
    do: <<1, library.encode!(map)::binary>>

  def decode(%TypeInfo{type: "json"}, json, _state, library),
    do: library.decode!(json)
  def decode(%TypeInfo{type: "jsonb"}, <<1, json::binary>>, _state, library),
    do: library.decode!(json)
end

I've also updated the README.

from postgrex.

zipme avatar zipme commented on August 24, 2024

@ericmj Thanks for the explanation!
@cmelgarejo You should use the above code instead, it works for me.

from postgrex.

cmelgarejo avatar cmelgarejo commented on August 24, 2024

Works even better now! :)

Thanks alot @ericmj and @zipme ! 🍻

from postgrex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.