Laravel 5.2时间戳

康斯坦丁诺斯·达斯卡洛普洛斯

好的,所以我尝试使用Laravel 5.2迁移制作一些简单的数据库架构。他们现在很基本。问题是创建的结果表无法在mysql中反映出我在架构生成器中指示的内容。例如:在此处输入图片说明创建下表(忽略该表后面的图片):在此处输入图片说明

我非常确定,如果我想允许空值,则应该使用nullableTimestampts(),也没有默认值,也没有“ on update CURRENT TIMESTAMP”等。

我在这里想念什么?aritsan命令非常简单:

php artisan make:migration create_table_institutes --create=institutes
php artisan migrate

我在Ubuntu 14.04,php7,Apache mysql上,这是我的种子代码:

DB::table('institutes')->insert([
    'name' => 'Ncbi',
    'base_curl' => 'eutils.ncbi.nlm.nih.gov/entrez/eutils/',
]);
博格丹

Actually the Schema Builder's timestamps method creates nullable timestamp columns by design since Laravel 5.2. The nullableTimestamps method is just a synonym which is more explicit as to how the timestamps are created, and all it does is call the timestamps method internally.

So if you need timestamp fields that are not nullable, then you need to manually create them. Also for the created_at column to be populated automatically with the current timestamp by MySQL, it needs a default values. So this:

$table->timestamps();

Should be replaced with this:

$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->dateTime('updated_at')->nullable();

For versions older that MySQL 5.6.5 where DATETIME columns cannot have a default value, a TIMESTAMP column can be used instead:

$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

虽然,如果您使用的是Eloquent,则除了created_at创建模型条目时始终填充之外,其他列也应该是,nullable因为它们只有在更新或软删除时才会获得分配的值。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章