Framework: Using UUID's for primary ID results in incorrect truncation

Created on 29 Nov 2016  Â·  7Comments  Â·  Source: laravel/framework

  • Laravel Version: 5.3.23
  • PHP Version: 7.0.13
  • Database Driver & Version: Postgres, 9.6 (don't think this matters)

Description:

If I have a table with a UUID column, and echo that column or render it in a Twig template, the value gets truncated to the first two characters.

Steps To Reproduce:

Create a table with a UUID column, for example

CREATE TABLE conferences(
    id UUID PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now()
);

and then have a Eloquent model:

class Conference extends Model {}

If I do for example

$conference = Conference::findOrFail('54a88511-ff47-4dd8-a418-0f6a4bff24bd');
echo "id: $conference->id";
error_log($conference->id);

Only the first two characters are printed.

id: 54
54

The same holds if I pass this value to a Twig template.

If I var_dump the model instance, I see that id contains the entire string, so I'm not sure where it's being truncated.

I can get the right value out if I do $this->attributes['id'] from inside the model, but I don't have access to this from outside. I'm working around this for the moment by adding a correct_id function to my model, which returns $this->attributes['id'].

Most helpful comment

Make sure you have properly defined the primary key in your model:

protected $primaryKey = 'id';
protected $keyType = 'string';
public $incrementing = false;

All 7 comments

Ah. If I name the column something different (I also have a group_id key) it gets rendered just fine so I imagine Laravel has some kind of code that incorrectly assumes the id field is an integer.

Make sure you have properly defined the primary key in your model:

protected $primaryKey = 'id';
protected $keyType = 'string';
public $incrementing = false;

Please continue on the forums, Twig is not supported by default in Laravel and thus all Twig related issues shouldn't be discussed in laravel's core repository.

On the forums you'll find many others who use Twig that are able to help you.

Thanks :)

Hello, @themsaid !
This doesn't seem to have much to do with Twig, except it's breefly mentioned in the OP.
It could as well be a blade template, or just a plain code example.

If so, can you please try $conference->id in a controller and see if you get the full string? or using blade? If it works there then Twig trims the value in a way.

Sorry - I did try calling it in normal code both with echo and with
error_log and got the truncation. I got confused because var dump reports
the key is there and it's a string. I'll try those properties on the model,
thanks!

On Tue, Nov 29, 2016 at 05:11 Mohamed Said notifications@github.com wrote:

If so, can you please try $conference->id in a controller and see if you
get the full string? or using blade? If it works there then Twig trims the
value in a way.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/laravel/framework/issues/16576#issuecomment-263566146,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAOSI373aNp5n1iBSo3wVYZvCEdLKYCfks5rDCSGgaJpZM4K-bnR
.

Maybe this is a documentation problem, https://laravel.com/docs/5.3/eloquent does not mention $keyType. I'll file a ticket there.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  Â·  3Comments

CupOfTea696 picture CupOfTea696  Â·  3Comments

gabriellimo picture gabriellimo  Â·  3Comments

shopblocks picture shopblocks  Â·  3Comments

felixsanz picture felixsanz  Â·  3Comments