itsource

두 개의 타임스탬프 열이 있는 테이블을 만드는 동안 Larabel 오류가 발생했습니다.

mycopycode 2022. 9. 28. 00:08
반응형

두 개의 타임스탬프 열이 있는 테이블을 만드는 동안 Larabel 오류가 발생했습니다.

저는 Laravel 6.6에 다음과 같은 정의를 가진 테이블을 만들었습니다.

public function up()
{
    Schema::create('quarters', function (Blueprint $table) {
        $table->integer('quarter_id')->unsigned();
        $table->integer('year')->unsigned();
        $table->integer('quarter_num')->unsigned();
        $table->timestamp('valid_from');
        $table->timestamp('valid_to'); // <------ error on this line
        $table->string('description')->nullable();
        $table->timestamps();
        $table->primary('quarter_id');
    });
} 

이행 명령어를 실행하면 다음 오류가 나타납니다.

\: [ 42000 :Query Exception : SQLSTATE [ 42000 ]: 또는 위반: " 기본값입니다(SQL: create table " 1067 "valid_to").quarters )quarter_id null, int unsigned not null, int unsigned null, null unsigned null.year null, int unsigned not null, int unsigned null, null unsigned null.quarter_num null, int unsigned not null, int unsigned null, null unsigned null.valid_from늘이 .늘이 아닙니다.valid_to늘이 .늘이 아닙니다.descriptionvarchar(255) null,created_at null, "timestamp" null,updated_at null) 집합 ' collate utf8mb4 collate 'utf8mb4_ci')

다음은 Allustic에 의해 생성된SQL입니다.

CREATE TABLE `quarters`(
    `quarter_id` INT UNSIGNED NOT NULL,
    `year` INT UNSIGNED NOT NULL,
    `quarter_num` INT UNSIGNED NOT NULL,
    `valid_from` TIMESTAMP NOT NULL,
    `valid_to` TIMESTAMP NOT NULL,
    `description` VARCHAR(255) NULL,
    `created_at` TIMESTAMP NULL,
    `updated_at` TIMESTAMP NULL
) DEFAULT CHARACTER SET utf8mb4 COLLATE 'utf8mb4_unicode_ci'

한 은, 가 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★.valid_to이치노 '아주머니'의valid_to는 100%와 비슷합니다.valid_from 에러는 valid_from actually. 는 2개의 DB를 허용하지 .timestamp열!

, 「★★★★★★★★★★★★★★」를 했습니다.php artisan migrate --pretend을 사용하다

C:\xampp\htdocs\voiceit> php artisan migrate --pretend
CreateQuartersTable: create table `quarters` (`quarter_id` int unsigned not null, `year` int unsigned not null, `quarter_num` int unsigned not null, `valid_from` timestamp not null, `valid_to` timestamp not null, `description` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
CreateQuartersTable: alter table `quarters` add primary key `quarters_quarter_id_primary`(`quarter_id`)
CreatePeopleDatasTable: create table `people_datas` (`mt_id` bigint unsigned not null, `valid_for` int unsigned not null, `local_personal_id` bigint unsigned not null, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `date_of_birth` date null, `date_of_join` date null, `gender` varchar(1) not null, `location_type` varchar(1) not null, `created_at` timestamp null, `updated_at` timestamp null, `deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
CreatePeopleDatasTable: alter table `people_datas` add primary key `people_datas_mt_id_valid_for_primary`(`mt_id`, `valid_for`)
CreatePeopleDatasTable: alter table `people_datas` add constraint `people_datas_valid_for_foreign` foreign key (`valid_for`) references `quarters` (`quarter_id`)
CreatePeopleDatasTable: alter table `people_datas` add constraint `people_datas_gender_foreign` foreign key (`gender`) references `genders` (`id`)
CreatePeopleDatasTable: alter table `people_datas` add constraint `people_datas_location_type_foreign` foreign key (`location_type`) references `location_types` (`id`)
CreatePeopleDatasTable: alter table `people_datas` add index `people_datas_last_name_index`(`last_name`)

는 이 했습니다.timestamp로로 합니다.dateTime따라서 테이블 정의를 다음과 같이 변경함으로써 날짜와 시간이 필요하기 때문에 문제가 해결되었습니다.

Schema::create('quarters', function (Blueprint $table) {
             $table->integer('quarter_id')->unsigned();
             $table->integer('year')->unsigned();
             $table->integer('quarter_num')->unsigned();
             $table->dateTime('valid_from');
             $table->dateTime('valid_to');            
             $table->string('description')->nullable();
             $table->timestamps();
             $table->primary('quarter_id');
        });

어떻게 개가 수 요.not null테이블 내의 타임스탬프 컬럼.

MySql 및/또는 MariaDB의 타임스탬프 기본값에 대한 기본 동작은 첫 번째 타임스탬프 선언과 후속 타임스탬프 선언에 대해 다릅니다.

MariaDB에는 특정 테이블의 TIMESTAMP 데이터 유형을 사용하는 첫 번째 열에 대한 특별한 동작이 있습니다.특정 테이블에서 TIMESTAMP 데이터 유형을 사용하는 첫 번째 열에 대해 MariaDB는 자동으로 다음 속성을 열에 할당합니다.

기본 CURRENT_TIMESTamp ON 업데이트 CURRENT_TIMESTamp

즉, INSERT 또는 UPDATE 쿼리에서 열이 명시적으로 값을 할당받지 않은 경우 MariaDB는 열의 값을 현재 날짜와 시간으로 자동으로 초기화합니다.

INSERT 쿼리 및 UPDATE 쿼리에 대한 이 자동 초기화는 TIMESTAMP 데이터 유형을 사용하는 컬럼에 대해 DEFAULT CURRENT_TIMESTAMP 및 ON UPDATE CURRENT_TIMESTAMP 구를 지정하여 명시적으로 활성화할 수도 있습니다.이러한 구에서는 CURRENT_TIMESTamp(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP() 등 CURRENT_TIMESTAMP와 동의어가 허용됩니다.

https://mariadb.com/kb/en/library/timestamp/

Ergo, 첫 번째 타임스탬프(valid_from)의 자동 디폴트값을 가져옵니다.CURRENT_TIMESTAMP이는 타임스탬프필드에 적용할 수 있는 기본값입니다.두 번째 필드에는 데이터베이스에 허용되지 않는 다른 자동 기본값이 지정됩니다.

당면한 문제에 대한 해결책은 MariaDB의 제안을 따르고 명시적인 방법을 사용하는 것일 수 있습니다.CURRENT_TIMESTAMP두 번째 타임스탬프(또는 둘 다 사용 중)에 대한 기본값입니다.라라벨의 용어로 말하자면, 이것은 아마도$table->timestamp('valid_to')->useCurrent();.

이 점에서 중요한 것은 최종적으로 선택한 솔루션(타임스탬프보다 날짜 사용)이 문제에 대한 보다 적절한 해결책일 수 있다는 것입니다.타임스탬프는 메타데이터로만 주로 사용되는 다소 이상한 데이터 유형입니다.특히 Mysql과 MariaDB에서는 본질적으로 "2038년 문제"에 시달리고 있습니다.이것은 created_at 또는 updated_at 필드에는 18년 정도 문제가 되지 않지만 유효기간이 앞으로 몇 년이 될 수 있는 경우에는 문제가 될 수 있습니다.그리고 그것은 의도하지 않은 자동 디폴트 값에 더해서...

이 문제를 해결하려면 서버 변수 "explicit-defaults-for-timestamp"를 추가하거나 수정할 수 있습니다.ON으로 설정해야 합니다.

https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp

언급URL : https://stackoverflow.com/questions/59325560/laravel-error-in-creating-a-table-with-two-timestamp-columns

반응형