orm icon indicating copy to clipboard operation
orm copied to clipboard

🔑 UUID as primary key

Open radimvaculik opened this issue 3 years ago • 5 comments

Example into docs, how to easily setup UUID as primary key.

radimvaculik avatar Mar 02 '22 09:03 radimvaculik

DB column "id" uuid PRIMARY KEY,

Assign (read-only) id in entity constructor

/**
 * @property-read string $id {primary}
 */
class Foo extends Entity
{
	public function __construct()
	{
		parent::__construct();
		$this->setReadOnlyValue('id', (new \Symfony\Component\Uid\Ulid())->toRfc4122());
	}
}

That should be basically it.

mabar avatar Mar 02 '22 09:03 mabar

@mabar uuid data type was added in MariaDB 10.7. No data type support for MySQL (https://dev.mysql.com/blog-archive/mysql-8-0-uuid-support).

radimvaculik avatar Mar 02 '22 09:03 radimvaculik

I've never used UUID so I'm puzzled a little. What is the issue? From ORM pov there are two things:

  • mapping -> string to some type in DB -> this should be either ok or covered by doc
  • setting the id programatically -> Mabar's example is quite ok

So what do you miss?

hrach avatar Mar 02 '22 09:03 hrach

My example is for PostgreSQL. In MySQL char(36) should work just as fine, just way less performant.

I tried to use UUID with binary in MySQL and performance was almost like with native UUID, but IDs in queries became just unreadable. In Nextras ORM it can be done by setting mapping callbacks in DbalMapper, but be aware mapping of IDs is not fully supported - described in #291


So current limitation is IDs cannot be mapped to binary for database storage and cannot be mapped to a value object in php.

mabar avatar Mar 02 '22 09:03 mabar

Hm, that would be good reason to proceed with 291.

hrach avatar Mar 02 '22 10:03 hrach