Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upStoring uuid byte sequence to postgres. #179
Comments
|
Isn't it better to use native UUID type that PostgreSQL provides? I think that the guide you linked in your post is about MySQL/MariaDB which does not provide native UUID type. |
|
@N-Zaaijer, can you share the code you're using to try to store the UUID to the database? |
|
Thanks, @N-Zaaijer. I'm not very familiar with Laravel's Eloquent ORM. Can you provide a short reproducible script that I can use to run against Postgres to see the problem? |
|
I'll try and come up with something. |
|
Thanks. That will help a lot. All I need is an SQL |
|
Is this something you can work on? I added a native uuid field and a bytea field for comparison. |
|
I can try to make that work. Thanks! |
|
@ramsey did you get it to work? |
|
I stumbled upon something. The hex2bin doesn't convert all characters. Dumping the 'binary' results in b"\x11þîØ\x18·■\x1C¥õ\fx╬#\x029" for example. Here's an other test setup https://3v4l.org/ND2O3 |
|
The I haven’t been able to reproduce your issue yet. It will take some time, as I’m unfamiliar with Laravel and will need to learn how to set it up to run the script. Sorry for the delay. |
|
I ran into this issue as well today while using Symfony, Doctrine and Postgres. The error was thrown when inserting a row into a table with |
|
You don't need Laravel to reproduce it. You can just use Symfony + Doctrine + Postgres, define a doctrine entity with binary uuid field and try to insert something. I assume this is the same issue. |
|
Thanks, @enumag. I didn’t think this was specific to Laravel; the reproduction script requires Laravel, though. Would you be able to provide a small reproduction script that I can run outside of a framework to help debug this? Thanks! |
|
@ramsey Making framework-independent script is a little more complicated. Not sure when or if I'll have time for that. |
|
@ramsey Same for me with Symfony + Doctrine + Postrgres when i try to use binary type uuid. Im new to PostgreSQL so im wondering if the problem is in my configuration of database (i kept default posgre settings) or its in library. Any news about this problem ? |
|
It should be noted that when using postgresql's UUID data type, inserting/updating UUIDs stored in the field should specify the UUID in one of the following formats as a string rather then as the raw bytes in the SQL query: |
|
@jobe1986, I think the issue here is when attempting to store the bytes to a |
|
In that case, whilst I'm not familiar with Illuminate\Support\Facades\DB for postgresql database support in PHP, in the code I am familiar with I would use pg_escape_bytea() to escape the binary data first. Or I would look at pg_query_params() or a similar method to avoid directly concatenating the parameters into the SQL query itself. I offer this information as it may provide a hint or point you in the right direction. |
|
Thanks for that, @jobe1986. Do you know if bound parameters with PDO will escape the bytea data properly? |
|
I believe so, as https://stackoverflow.com/questions/5313066/bind-bytea-to-pgsql-pdo-prepared-statement-in-php5 suggests using PDO::PARAM_LOB as the data type for bindParam() |
|
In that case we just need to add I will try to test it tomorrow and send a PR if it works. |
|
Thanks to @enumag, this has been fixed in 1.4.2 of ramsey/uuid-doctrine. I'm leaving this issue open until I've been able to provide direction in how to address this problem in Laravel (and potentially other frameworks). Once I come up with a good, easy-to-explain solution, I'll update the README or the wiki with examples. |
|
We had to roll-back the changes made to ramsey/uuid-doctrine, since they broke MySQL/MariaDB implementations. I'll leave this issue open to continue researching and see if there's a solution I can offer. |
|
The solution is TO NOT use bytea and use native uuid type. The native type already stores uuid as a binary, but returns/requires the text representation for SQL |
Hi,
I'm currently having problems to store the optimized byte sequence to my postgres database. I keep getting the error
SQLSTATE[22021]: Character not in repertoire: 7 ERROR: invalid byte sequence for encoding "UTF8": 0xe7 0x87 0xf3I've followed the guide and used the OrderedTimeCodec in my uuid factory.
My database table alteration script:
DB::statement('ALTER TABLE entity_groups ADD uuid bytea');I'm using php 7.1, laravel 5.4 and postgres 9.4.
Thanks in advance.