Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using UUID's for primary ID results in incorrect truncation #16576

Closed
kevinburke opened this issue Nov 29, 2016 · 7 comments
Closed

Using UUID's for primary ID results in incorrect truncation #16576

kevinburke opened this issue Nov 29, 2016 · 7 comments

Comments

@kevinburke
Copy link

kevinburke commented Nov 29, 2016

  • 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'].

@kevinburke
Copy link
Author

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.

@kevinburke kevinburke changed the title UUID's being truncated incorrectly Using UUID's for primary ID results in incorrect truncation Nov 29, 2016
@sburina
Copy link

sburina commented Nov 29, 2016

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

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

@themsaid
Copy link
Member

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 :)

@sburina
Copy link

sburina commented Nov 29, 2016

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.

@themsaid
Copy link
Member

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.

@kevinburke
Copy link
Author

kevinburke commented Nov 29, 2016 via email

@kevinburke
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants