Tengo una migration para una tabla de viajes. El código es el siguiente:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateToursTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tours', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->unsigned();
$table->string('destino', 100);
$table->timestamp('inicio');
$table->timestamp('final');
$table->integer('duracion')->unsigned();
$table->text('detalles');
$table->timestamps();
/* El campo deleted_at */
$table->softDeletes();
/** La clave foránea para relacionar esta tabla con operators. */
$table->foreign('operator_id')->references('id')->on('operators');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tours');
}
}
El problema aparece con el campo final
. Lo quiero declarar como timestamp
. Lo hago con el campo inicio
y no da problemas, pero con el campo final
me da un error:
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'final' (SQL: create table `tours` (`id` int unsigned not null auto_increment primary key, `operator_id` int unsigned not null, `destino` varchar(100) not null, `inicio` timestamp not null, `final` timestamp not null, `duracion` int unsigned not null, `detalles` text not null, `created_at`
timestamp null, `updated_at` timestamp null, `deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')
at C:\xampp\htdocs\viajes\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'final'")
C:\xampp\htdocs\viajes\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\xampp\htdocs\viajes\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
El caso es que cojo la consulta que muestra el error, que es la siguiente:
create table `tours` (`id` int unsigned not null auto_increment primary key, `operator_id` int unsigned not null, `destino` varchar(100) not null, `inicio` timestamp not null, `final` timestamp not null, `duracion` int unsigned not null, `detalles` text not null, `created_at`
timestamp null, `updated_at` timestamp null, `deleted_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
La ejecuto directamente en PHPMyAdmin y funciona sin problemas.
Si el campo final
lo declaro como date
, en lugar de timestamp
, la migration se ejecuta sin problemas, pero, claro, no me queda el tipo de dato que quiero.
En el modelo Tour.php
tampoco parece haber nada fuera de lugar:
<?php
namespace App;
use App\Operator;
use App\Customer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Tour extends Model
{
use SoftDeletes;
protected $table = 'tours';
protected $fillable = [
'operator_id',
'destino',
'inicio',
'duracion',
'final',
'detalles'
];
protected $dates = [
'deleted_at'
];
public function operator()
{
return $this->belongsTo(Operator::class);
}
public function customers()
{
return $this->belongsToMany(Customer::class);
}
}
¿Alguien sabe a que puede deberse esto? Gracias a todos.