-
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
[9.x] InteractsWithDatabase::castAsJson($value)
incorrectly handles SQLite Database
#43863
Comments
This was originally added here: #34302 by @browner12. I'm not sure what the reasoning for a raw statement was but this indeed seems odd given our multiple DB engine support. Did you confirm the fix? Are you willing to PR it? (with a test if possible) |
I can for sure submit a PR this weekend with tests. The above posted solution works for me 👍 |
Sqlite does not support JSON columns at all, which is the reason I stopped using it completely for testing. @stevebauman, I guess I'm confused, how are your tests passing at all? Doesn't Sqlite throw an error when you try to migrate a JSON column? |
@browner12 The Laravel schema builder will create a |
yah, this PR is starting to come back to me a little. it's definitely not a perfect solution, as it'd be best to know if the field in question is truly a JSON field. Are there any other DB engines that don't support JSON types? Would we want to be more generic with something like: public function castAsJson($value)
{
if ($value instanceof Jsonable) {
$value = $value->toJson();
} elseif (is_array($value) || is_object($value)) {
$value = json_encode($value);
}
if (!DB::connection()->supportsJson()) {
return $value;
}
$value = DB::connection()->getPdo()->quote($value);
return DB::raw("CAST($value AS JSON)");
} and then adjust the connection classes a little? |
That’s a good point @browner12, I’m not sure. Let me dig into it a bit and see which built-in Laravel DB drivers support JSON columns 👍 |
MySQL added JSON support in v5.7PostgresSQL added JSON support in v9.2https://www.postgresql.org/docs/current/datatype-json.html SQLServer has no native JSON datatype, but allows you to work with JSON since v2016SQLite also has no native JSON datatype, but actually added JSON support in v3.38https://tirkarthi.github.io/programming/2022/02/26/sqlite-json-improvements.html So here lies the issue. We're not only having to check the driver, but also the version being used. Rather than adjusting the |
Yea I agree with this. Having it inside of the grammar makes sense 👍 Would you like me to tackle the PR if you got your hands full at the moment? |
sure thing. tag me in it and I'll try and provide some feedback. |
Going to close this since we can assume SQLite isn't supported yet for this function. Feel free to send in a PR when you can. Thanks all. |
Description:
When using SQLite to test locally, calling
InteractsWithDatabase::castAsJson
in a test incorrectly casts the value to a raw DB expression, containingCAST($value AS JSON)
.Steps To Reproduce:
When running this with a MySQL connection, the assertion passes fine.
This causes issues since I run my tests locally under SQLite for speed, and then in GitHub Actions with MySQL when pushing to master.
Reference:
framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php
Lines 173 to 184 in d23de5f
Proposed solution:
The text was updated successfully, but these errors were encountered: