-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Eloquent unable to save Postgres arrays #27616
Comments
I'll need someone to verify this on PostGres. |
This is not supported. Eloquent can't know whether you want to store a PostgreSQL array or a JSON string. We could stop having the You'll have to add a custom accessor and mutator: class Test extends Model {
public function getFieldAttribute($value) {
return json_decode(str_replace(['{', '}'], ['[', ']'], $value));
}
public function setFieldAttribute($value) {
$this->attributes['field'] = str_replace(['[', ']'], ['{', '}'], json_encode($value));
}
} |
So the lines like this... case 'array':
case 'json':
return $this->fromJson($value); from the which ignored the differences between those two data types, are the issue. |
My example is meant for the integer arrays from your example. You can also try this package: https://github.com/asmiarowski/laravel-postgres Array columns are only supported by PostgreSQL and not very widely used (my impression). So they simply weren't considered when that code was written. Not so simple to change it now. |
That kinda supports my point ( I think ). What exactly are Array columns outside of PGSQL? MySQL doesn't have them. So is this problem code even executing in other scenarios? |
How do you mean? The |
oh cuz |
is there another attribute system other than |
It's not a "fake way", MySQL and PostgreSQL do have native JSON column types. |
I'm not talking about JSON at all though, I'm talking about ARRAY |
so this fake |
JSON is a reasonable way to store arrays, especially when it's supported by all databases. It doesn't break PostgreSQL arrays, they wouldn't work natively either way. |
ok but still this is not a conversation about |
information on postgres database arrays. PGSQL docs |
I'm familiar with PostgreSQL arrays. The point of the I assume that the documentation suggests to use |
Since it seems that this isn't really a bug but more of a request for more native Postgres support, I think it's best that this is moved to the ideas repo. |
eloquent already supports postgres, but this feature was built and breaks a postgres feature that already existed before eloquent was made. |
To me it seems indeed that:
That said, I agree with @driesvints
Btw, there's is laravel/ideas#1354 and probably others I couldn't find quickly. |
this So from my point of view, sorta like the It kinda seems like if there is going to be faked ideas then they should be inside the database specific classes, rather than the generic classes. Let alone that if MySQL added actual array fields, which is apparently part of the SQL Standard. Then this faked If |
yeah that custom castings idea you referenced is what I picture the current So that custom casting that was built into eloquent is breaking generic model/database functionality... So is eloquent intended to hold custom logic that requires non-uniform aspects to be added to in sync between the DB and the PHP? Cuz in this scenario, the |
Please see for a current PR proposal to support custom casting => #30958 |
@mfn seems taylor is blocking fixes... |
its 2024. |
Thank you for saving my time. |
Isn't this possible with custom caster, which make sure the raw attribute format matches what pgsql needs? |
Description:
The eloquent code is treating a database
array
and ajson
object as if they are the same. But in PostGres the format for an array like anINTEGER[]
needs formatting to be asbut because of current setup using json_encode, the result is
The minor but crucial difference being the [ and the {
Steps To Reproduce:
with Model cast defining array:
will generate...
which gives the error
The text was updated successfully, but these errors were encountered: