In my Laravel version 10 application, I have created users table using migration. How I want to add new columns. I have this:
<code>public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
$table->enum('type', ['monthly', 'annual']); // Type d'abonnement
$table->timestamp('start_date');
$table->timestamp('end_date');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscriptions');
}
};
</code>
<code>public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
$table->enum('type', ['monthly', 'annual']); // Type d'abonnement
$table->timestamp('start_date');
$table->timestamp('end_date');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscriptions');
}
};
</code>
public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
$table->enum('type', ['monthly', 'annual']); // Type d'abonnement
$table->timestamp('start_date');
$table->timestamp('end_date');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscriptions');
}
};
When I did:
<code>php artisan migrate
</code>
<code>php artisan migrate
</code>
php artisan migrate
I got this error:
<code>SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'end_date'
(Connection: mysql, SQL: create table `subscriptions`
(`id` bigint unsigned not null auto_increment primary key, `student_id` bigint unsigned not null, `course_id` bigint unsigned not null,
`type` enum('monthly', 'annual')
not null, `start_date` timestamp not null, `end_date` timestamp not null, `created_at` timestamp null, `updated_at` timestamp null)
default character set utf8mb4 collate 'utf8mb4_unicode_ci')
</code>
<code>SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'end_date'
(Connection: mysql, SQL: create table `subscriptions`
(`id` bigint unsigned not null auto_increment primary key, `student_id` bigint unsigned not null, `course_id` bigint unsigned not null,
`type` enum('monthly', 'annual')
not null, `start_date` timestamp not null, `end_date` timestamp not null, `created_at` timestamp null, `updated_at` timestamp null)
default character set utf8mb4 collate 'utf8mb4_unicode_ci')
</code>
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'end_date'
(Connection: mysql, SQL: create table `subscriptions`
(`id` bigint unsigned not null auto_increment primary key, `student_id` bigint unsigned not null, `course_id` bigint unsigned not null,
`type` enum('monthly', 'annual')
not null, `start_date` timestamp not null, `end_date` timestamp not null, `created_at` timestamp null, `updated_at` timestamp null)
default character set utf8mb4 collate 'utf8mb4_unicode_ci')
How do I get it resolved?
Thanks
1
This error is comming because MySQL timestamp columns require a
valid default value
<code>public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
$table->enum('type', ['monthly', 'annual']);
$table->timestamp('start_date')->useCurrent();
$table->timestamp('end_date')->nullable(); // Make end_date nullable
$table->timestamps();
});
}
</code>
<code>public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
$table->enum('type', ['monthly', 'annual']);
$table->timestamp('start_date')->useCurrent();
$table->timestamp('end_date')->nullable(); // Make end_date nullable
$table->timestamps();
});
}
</code>
public function up(): void
{
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->constrained('students')->onDelete('cascade');
$table->foreignId('course_id')->constrained('courses')->onDelete('cascade');
$table->enum('type', ['monthly', 'annual']);
$table->timestamp('start_date')->useCurrent();
$table->timestamp('end_date')->nullable(); // Make end_date nullable
$table->timestamps();
});
}