Laravel 创建具有两个时间戳列的表时出错

2024-01-09

我在 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');
    });
} 

当我运行迁移命令时,出现以下错误。

Illuminate\Database\QueryException : SQLSTATE[42000]: 语法错误或 访问冲突:1067 “valid_to”的默认值无效(SQL: 创建表quarters (quarter_idint 无符号不为空,year无符号整型 不为空,quarter_numint 无符号不为空,valid_from时间戳不为空,valid_to时间戳不为空,descriptionvarchar(255) 空,created_at时间戳为空,updated_at时间戳 null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

这是 Eloquent 生成的 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_to100%相似于valid_from,并且它不会抛出该错误valid_from柱子。实际上,数据库似乎不允许两个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 to 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表中的时间戳列。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel 创建具有两个时间戳列的表时出错 的相关文章

随机推荐

  • 如何在Vuex中深度克隆状态并回滚?

    在 Vuex 中 我想拍摄树中对象属性的快照 克隆 修改它 然后可能回滚到以前的快照 背景 在应用程序中 用户可以在应用某些更改之前尝试它们 应用更改时 它们应该影响主 vuex 树 用户还可以单击 取消 放弃更改并返回到之前的状态 Exa
  • 相关查找字段外键在内联 Django 中不起作用

    我的 tabularinline 字段有问题 我有这样的模型 class Product models Model class Pemesanan models Model produks models ManyToManyField Pr
  • axios 在 componentDidMount 中获取数据后如何拍摄笑话快照?

    要测试的组件 class Carousel extends React Component state slides null componentDidMount gt axios get https s3 amazonaws com ra
  • 拖动数据点并提交值

    On page jqPlot http www jqplot com deploy dist examples customHighlighterCursorTrendline html有一个在 jqPlot 图表上拖动数据点的示例 我如何
  • Ajax v. 在 HTML 中包含数据

    我将 JavaScript 与 jQuery 结合使用 与 Django 后端对话 有一些 UI 需求需要 Ajax 因为在用户提供一些输入之前我们无法知道要发送哪些数据 然而 还有其他在模板时已知的数据 直接将该数据包含在模板中而不是使用
  • PHP ↔ Perl 接口或绑定

    您推荐哪种 PHP Perl 接口或绑定 我需要能够从 PHP 脚本运行位于 Perl 文件中的 Perl 函数并获取返回值 我已经找到了PECL perl 包 http pecl php net package perl但我不确定它的可靠
  • 64 位上 int 与 size_t

    将代码从 32 位移植到 64 位 很多地方都有 int len strlen pstr 这些现在都会生成警告 因为 strlen 返回 64 位的 size t 而 int 仍然是 32 位 所以我一直用它们替换 size t len s
  • 如何在提取文本之前识别可能损坏的 pdf 页面?

    TL DR 我的工作流程 下载PDF 使用将其拆分为页面pdftk https linux die net man 1 pdftk 使用提取每个页面的文本pdf转文本 https linux die net man 1 pdftotext
  • nginx 从rails 反向代理到wordpress

    我有一个 Ruby on Rails 应用程序和一个托管在不同 EC2 实例上的 Wordpress 博客 我正在尝试使 Wordpress 博客充当 Rails 应用程序的子文件夹 example com blog 而不是 blog ex
  • 在 Mac OS X 中链接动态库 (libjvm.dylib)(rpath 问题)

    我确实有一个需要链接的应用程序libjvm JDK 中的库需要进行 JNI 绑定 当我说出地点时libjvm dylib using L它成功编译和链接 但是 当我运行二进制文件时 我得到 dyld Library not loaded r
  • 如何在 Angular 中模拟 HTTP 请求?

    我检查了很多文章和答案 但似乎没有找到正确的方法来模拟HTTP Requests对于我的方法 我想测试我的frontend应用程序独立于backend 这是我拥有的方法类型 private getProfile this http get
  • 如何在每次启动时运行我自己的脚本

    我有一个问题 如何在 Ubuntu 中每次启动时运行自己的 bash 脚本 假设我有一个正在执行特定类型工作的脚本 现在我希望它在启动 Ubuntu 系统时自动运行 你应该学习如何使用暴发户 看this http upstart ubunt
  • 从 F# 中使用“params”调用泛型函数 (Observable.StartWith)

    Edit 请注意 正如 Daniel 和 latkin 在下面的回答和评论中指出的那样 这个问题涉及 F 中的一个错误 该错误似乎已于 2014 年初修复 我正在尝试为 Observable StartWith 编写一个咖喱包装器 我正在使
  • WordPress 中的 woocommerce 返回始终与产品类型一样简单

    我尝试获取分组产品的类型 但如果我使用 WC Product Factory woocommerce 返回空或始终 简单 当我使用时 the product new WC Product 2886 echo the product gt p
  • django send_mail() 函数需要几分钟

    我正在尝试在views py 文件中的函数中发送电子邮件 我已按照与此处相同的方式在我的设置文件中设置了电子邮件 Python Django Gmail SMTP 设置 https stackoverflow com questions 1
  • Java 1.5 命令行密码屏蔽

    All 我们的服务器正在运行 Java 1 5 我在尝试屏蔽来自命令行的用户输入时遇到了困难 我正在执行一个 jar 文件 java jar my jar 并通过 printlns 通过命令行提示符进行工作 我无法使用 Java 控制台 T
  • CDI 将服务注入 JPA 托管实体

    我确信这与这个问题 https stackoverflow com q 8512628 206466但是关于这个问题的OP有一些我不确定对DI是否有意义的场景 所以这就是我的理解 尝试将 JPA 实体与 CDI Bean 混合通常不是一个好
  • 图像 getWidth 和 getHeight 不适当地返回 -1

    为什么会这样 URL url MinecraftPlatformGame class getResource images diamondPick png image Toolkit getDefaultToolkit getImage u
  • Jquery 检查值是否为数字

    我想知道是否有人有一个快速而肮脏的 jquery 方法来检查一个值是否是数字 我正在考虑使用正则表达式类型方法来检查值 如果没有 则不要提交表单 我正在使用 jquery 验证 但我在加载 jquery 验证时遇到了问题 我只有一个值 我想
  • Laravel 创建具有两个时间戳列的表时出错

    我在 Laravel 6 6 中创建了一个具有以下定义的表 public function up Schema create quarters function Blueprint table table gt integer quarte