The database exceeds the length limit, even after running the migration files

56 views Asked by At

Laravel Version

10.15.0

PHP Version

8.1

Database Driver & Version

Ver 8.0.33 for Linux on x86_64

Description

My database has a 'province_code' field with a length of 4 varchar.

When trying to save the value 'PT-11,' it resulted in an error.

After running the migration file and increasing the length to 255, the error still persists.

Upon checking, the field length is 255, and new records have been added.

migrate file :

   public function up(): void
    {
        //
        Schema::table('customers', function (Blueprint $table) {
            $table->string('country_code',255)->nullable()->comment('国家缩写')->change();
            $table->string('province_code',255)->nullable()->comment('省/州缩写')->change();
        });
    }

message (using fake data) :

       "getMessage": "SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'province_code' at row 1 (Connection: mysql, SQL: insert into `customers` (`email`, `root_id`, `team_id`, `name`, `phone`, `country`, `province`, `city`, `province_code`, `address`, `other_address`, `zip_code`, `user_id`, `email_account_id`, `updated_at`, `created_at`) values ([email protected], 0, 0, kdoask 320, +3511932000011, Portugal, Lisboa, djkasld dalsjd dkaskda oewqpoe, PT-11, FDSFSD FDSFDS dsdsfds, lfpslfps dasdad dasdaseq, 220011, ?, 1, 2023-10-30 15:25:39, 2023-10-30 15:25:39))",
        "method": "App\\Jobs\\SaveCustomer::handle",
        "line": 795

fillable attribute (part) :

  protected $fillable = [
        'name',
        'email',
        'address',
        'city',
        'country',
        'country_code',
        'province',
        'province_code',

The bug is running this code :

   Customer::updateOrCreate(
                [
                    'email'   => $shipping['email'],
                    'root_id' => $this->ticket->root_id,
                    'team_id' => $this->ticket->team_id,
                ],

                $shipping + [
                    'root_id'          => $this->ticket->root_id,
                    'team_id'          => $this->ticket->team_id,
                    'user_id'          => $this->ticket->user_id,
                    'email_account_id' => $this->ticket->email_account_id,
                ]);

Steps To Reproduce

  1. First run migtate (created field , length is 4)
  2. Code is writed (copy content of title"The bug is running this code")
  3. run migtate (changed field , length is 255)
  4. Run code

I checked the database field information and found that the length is 255,

and it can insert a value of 'PT-11' into the row.

However, running this code results in an exception,

which means it's triggered multiple times.

In the past, it was triggered multiple times without encountering this issue.

Succes is native SQL, but error is running code

I did not understand the question.

0

There are 0 answers