Possible error in "Working with databases" (Yii3 Guide)

I’m trying the code that is in the guide and in the section about working with database I think I found an error.

This is the code that generates the error that does not allow the record to be saved (in the EditAction file):

$id = $isNew ? Uuid::uuid7()->toString() : $page->id;

The reason is that the id column is a binary[16], but the toString() method generates an ASCII string that is longer than 16 chars.

Changing the code to

 $id = $isNew ? Uuid::uuid7()->getBytes() : $page->id;

fixes the problem.
I’m using MySql and not Postresql, but I don’t think this should matter.

1 Like

I’ve tested the initial code with PostgreSQL and it worked. Need to check if binary is fine…

See A unversal way to work with UUIDs · Issue #1152 · yiisoft/db · GitHub. Thanks for leetting me know.

Thanks … interesting to see that MySql and Posgresql behave differently in this case.

Hi, i tried to create migration and tried to migrate:up but i got these errors

PS D:\codes\php\src\yii3\test_1> ./yii migrate:up

Creating migration history table "migration"...
-----------------------------------------------

2026-05-04 05:36:55.764300 [error][application] Yiisoft\Db\Exception\Exception: SQLSTATE[42501]: Insufficient privilege: 7 ERROR:  permission denied for schema public at character 14
The SQL being executed was: CREATE TABLE "migration" (
        "id" serial PRIMARY KEY,
        "name" varchar(180) NOT NULL,
        "apply_time" integer NOT NULL
) in D:\codes\php\src\yii3\test_1\vendor\yiisoft\db\src\Exception\ConvertException.php:45 while running console command "migrate:up".

how to tell the migration to do creating table migration to our user schema, not public schema?

I already set privileges to my user in postgresql

-[ RECORD 4 ]-----+---------------------------------------
Name              | public
Owner             | pg_database_owner
Access privileges | pg_database_owner=UC/pg_database_owner+
                  | =U/pg_database_owner                  +
                  | superman=UC/pg_database_owner
Description       | standard public schema

my user : superman for this testing

Hi, I’m already solved this problem by doing this in postgresql database by creating own schema for your user and set the search_path.

CREATE SCHEMA myapp AUTHORIZATION superman;
ALTER ROLE superman SET search_path = myapp;

You’re right about the root cause.

  • toString() returns a 36-char UUID (too long for BINARY(16)), so MySQL rejects it.
  • getBytes() correctly returns a 16-byte binary value, which matches BINARY(16).

fix

Use this for BINARY(16) columns:

$id = $isNew ? Uuid::uuid7()->getBytes() : $page->id;