Yêu cầu thg 4 22, 2021 4:47 SA 167 1 3
  • 167 1 3
+2

Lỗi khi thực viết migration change field trong Laravel với postgresql

Chia sẻ
  • 167 1 3

Hi anh chị,

Em đang làm dự án sử dụng laravel và postgresql. Khi e viết migration tạo bảng với tên bảng dạng in hoa ( cái này dự án bắt buộc để connect với bên thứ 2), ví dụ tên bảng Employees, lúc tạo thì bình thường, nhưng khi e viết migration để chỉnh sửa column trong bảng thì phát sinh lỗi : code migration:

  Schema::table('Employees', function (Blueprint $table) {
            $table->string('DateOfBirth')->nullable()->change();
        });

Lỗi:

 Doctrine\DBAL\Exception\TableNotFoundException

  An exception occurred while executing 'SELECT obj_description('Employees'::regclass) AS table_comment;':

SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "employees" does not exist
LINE 1: SELECT obj_description('Employees'::regclass) AS table_comme...

Không biết có anh chị nào gặp trường hợp này rồi có thể support e với được không ạ!

thg 4 22, 2021 5:19 SA

Tức là lúc chạy lệnh migrate thì nó bắn lỗi như kia à bạn?

Avatar Dương Siêu @duongdosieu224
thg 4 22, 2021 6:27 SA

@huukimit dạ đúng rồi, viết xong chạy php artisan migrate là bị

thg 4 22, 2021 6:34 SA

@duongdosieu224 Cho mình hỏi cái:

  1. Bạn dùng laravel version bao nhiêu vậy nhỉ?
  2. Cái code sửa cột DateOfBirth là ở file migration mới hay bạn sửa trực tiếp vào file migration tạo bảng Employees đã có trước đó vậy?
  3. Bạn show full source của 2 file migration tạo table Employees và file migration sửa cột DateOfBirth được ko nhỉ?
Avatar Dương Siêu @duongdosieu224
thg 4 22, 2021 12:10 CH

@huukimit

  1. Mình dùng laravel ver7
  2. Mình tạo migration mới
  3. File migration tạo:

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateEmployeesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Employees', function (Blueprint $table) {
            $table->uuid('Id')->unique();
            $table->primary('Id');
            $table->string('FullName')->nullable();
            $table->date('DateOfBirth')->nullable();
            $table->timestamp('CreationTime', 0)->nullable();
            $table->timestamp('LastModificationTime', 0)->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('Employees');
    }
}

file migration sửa

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class ChangeFieldDateOfBirthToEmployeesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('Employees', function (Blueprint $table) {
            $table->string('DateOfBirth')->nullable()->change();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('Employees', function (Blueprint $table) {
            $table->date('DateOfBirth')->nullable()->change();
        });
    }
}

3 CÂU TRẢ LỜI


Đã trả lời thg 4 23, 2021 3:22 SA
Đã được chấp nhận
+3

Vì nó tự đổi sang lowercase đó, cái này do postgresql chứ không phải do laravel.

Schema::table('"Employees"', function (Blueprint $table) {
            $table->string('"DateOfBirth"')->nullable()->change();
        });

try it!

Chia sẻ
Avatar Dương Siêu @duongdosieu224
thg 4 23, 2021 3:31 SA
Đã trả lời thg 4 23, 2021 2:47 SA
+2

Bạn thử override getTable() bên trong model xem

public function getTable()
{
    return ucfirst(parent::getTable());
}
Chia sẻ
Avatar Dương Siêu @duongdosieu224
thg 4 23, 2021 2:53 SA

bên trong migration chứ ạ

Đã trả lời thg 5 10, 2021 10:18 SA
0

Cái code sửa cột DateOfBirth là ở file migration mới hay bạn sửa trực tiếp vào file migration tạo bảng Employees đã có trước đó vậy?

https://kyoo.in/cat/courses/

Chia sẻ
Avatar Dương Siêu @duongdosieu224
thg 5 28, 2021 4:00 SA

migration mới á bạn

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí