Skip to content

[12.x] Add support for native JSON/JSONB column types in SQLite Schema builder #54991

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

Merged
merged 4 commits into from
Mar 13, 2025

Conversation

fuwasegu
Copy link
Contributor

@fuwasegu fuwasegu commented Mar 12, 2025

redo of #54984

Overview

SQLite has supported JSON data type as a built-in feature since version 3.38.0, but prior to that, users needed to opt-in by installing an extension.
Additionally, native support for JSONB type began with version 3.45.
Since Laravel 12 supports SQLite 3.26.0 and above, it should not be designed with the assumption that JSON/JSONB support is always available.
While Query\Grammars\SQLiteGrammar::class includes support for JSON functions, Schema\Grammars\SQLiteGrammar::class does not offer this option.
This PullRequest allows injecting configuration from the database connection config to enable the use of native JSON columns when defining Schema.

Details

This feature allows the following configuration in config/database.php:

'sqlite' => [
    'driver' => 'sqlite',
    'url' => env('DATABASE_URL'),
    ...,
    'use_native_json' => true,
    'use_native_jsonb' => true,
],

JSON

By setting use_native_json in a connection with sqlite driver, the following DDL changes will occur:

use_native_json = false (default)

Schema::create('sample', function (Blueprint $table) {
    $table->json('column_1');
});
create table sample
(
    column_1 text not null
);

use_native_json = true

Schema::create('sample', function (Blueprint $table) {
    $table->json('column_1');
});
create table sample
(
    column_1 json not null
);

JSONB

The same applies to JSONB:

use_native_jsonb = false (default)

Schema::create('sample', function (Blueprint $table) {
    $table->jsonb('column_1');
});
create table sample
(
    column_1 text not null
);

use_native_jsonb = true

Schema::create('sample', function (Blueprint $table) {
    $table->jsonb('column_1');
});
create table sample
(
    column_1 jsonb not null
);

@taylorotwell taylorotwell merged commit 95a8561 into laravel:12.x Mar 13, 2025
34 checks passed
@fuwasegu fuwasegu deleted the feat/add-sqlite-json-type branch March 13, 2025 15:38
@fuwasegu
Copy link
Contributor Author

Thanks for the merge.
The schema hierarchy seems to have disappeared and use_native_json/use_native_jsonb is now placed at the top level, so I have modified the PullRequest description.

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

Successfully merging this pull request may close these issues.

2 participants