Skip to content

SQL Server String field longer than 4000 characters #56064

Closed
@tomhafner

Description

@tomhafner

Laravel Version

11.36.1

PHP Version

8.3.15

Database Driver & Version

SQL Server

Description

We have the problem that text fields longer than 4000 characters are not created correctly on the SQL Server, because instead of e.g. NVARCHAR(5000) we must of course use NVARCHAR(MAX)...
We use:

Schema::create('modules', function (Blueprint $table) {
               $table->unsignedBigInteger('id')->primary();
               $table->string('name', 5000)->nullable()->default(null);
               $table->longText('options')->nullable()->default(null);
               ...

Is there a way to configure this?
If not, you could simply correct it in https://github.com/laravel/framework/blob/12.x/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php:

protected function typeString(Fluent $column)Add commentMore actions
    {
        if ($column->length > 4000){
            return 'nvarchar(max)';
        } else {
            return "nvarchar({$column->length})";
        }
    }

Steps To Reproduce

Create a table with an string field and length > 4000 on MS SQL Server

Schema::create('modules', function (Blueprint $table) {
               $table->unsignedBigInteger('id')->primary();
               $table->string('name', 5000)->nullable()->default(null);
               $table->longText('options')->nullable()->default(null);
               ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions