Cannot rename date time column with Laravel Migrtions - php

I want to simply rename my one column although it keeps saying
SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'sendReminderCouple48' (SQL: ALTER TABLE sendIntakes CHANGE sendReminderCouple36 sendReminderCouple48 DATETIME DE
FAULT 'NULL')
Currently its sendReminderCouple36 - DATETIME - NULLABLE
All I want to do is rename it to sendReminderCouple48
public function up()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple36', 'sendReminderCouple48');
});
}
public function down()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple48', 'sendReminderCouple36');
});
}
Note: I do not want to do any strict changes in my config file.

This is probably related to issue #22050 on the Laravel framework. Are you running MariaDB as your database engine?
Following the advice in the thread, you might try changing the default value on the column before doing the rename call:
Schema::table('products', function (Blueprint $table) {
$table->string('name')->default(null)->change();
});
Schema::table('products', function (Blueprint $table) {
$table->renameColumn('name', 'description');
});
Schema::table('products', function (Blueprint $table) {
$table->string('description')->default(null)->change();
});

I fixed it by changing the type to a text - rename - then back to date time
public function up()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->text('sendReminderCouple36')->default(null)->nullable()->change();
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple36', 'sendReminderCouple48');
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->datetime('sendReminderCouple48')->default(null)->nullable()->change();
});
}
public function down()
{
Schema::table('sendIntakes', function (Blueprint $table) {
$table->text('sendReminderCouple48')->default(null)->nullable()->change();
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->renameColumn('sendReminderCouple48', 'sendReminderCouple36');
});
Schema::table('sendIntakes', function (Blueprint $table) {
$table->datetime('sendReminderCouple36')->default(null)->nullable()->change();
});
}

Related

Why do I need to call Schema::table in Laravel more than once?

I'm on a Udemy course about Laravel 7. In a particular class, I need to rename the column of a table called site_contacts. When I try to simplify the code and I and put two instructions in a callback the error occurs: There is no column with name 'contact_reason_id' on table 'site_contacts'. This occurs when you reach the statement to change the column type. But, if I write separately, as it is in the code, migration runs without problems.
I would like to understand why it needs to be done this way and if there's any better way. Initially, I think the class needs to start with its updated internal attributes to proceed with the statements, otherwise it doesn't know that the column is under another name.
This code does not work.
public function up()
{
Schema::table('site_contacts', function (Blueprint $table) {
// There is no column with name 'contact_reason_id' on table 'site_contacts'.
$table->renameColumn('contact_reason', 'contact_reason_id');
$table->unsignedBigInteger('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
});
}
This code works.
public function up()
{
Schema::table('site_contacts', function (Blueprint $table) {
$table->renameColumn('contact_reason', 'contact_reason_id');
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->unsignedBigInteger('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->foreign('contact_reason_id')->references('id')->on('contact_reasons');
});
}
public function down()
{
Schema::table('site_contacts', function (Blueprint $table) {
$table->dropForeign('site_contacts_contact_reason_id_foreign');
$table->dropIndex('site_contacts_contact_reason_id_foreign');
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->integer('contact_reason_id')->change();
});
Schema::table('site_contacts', function (Blueprint $table) {
$table->renameColumn('contact_reason_id', 'contact_reason');
});
}
Thanks.

Laravel: How to solve "SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint"

I am making migrations where there is a particular migration that maps one table with various other tables using a foreign key. and I am getting the following error while running the migration.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table bookmaps add constraint bookmaps_subject_id_foreign foreign key (subject_id) references subject (id) on delete cascade)
Here is the migration that is creating the error:
public function up()
{
Schema::create('bookmaps', function (Blueprint $table) {
$table->unsignedBigInteger('book_id')->unique();
$table->unsignedBigInteger('subject_id')->nullable();
$table->unsignedBigInteger('grade_id')->nullable();
$table->unsignedBigInteger('author_id')->nullable();
$table->unsignedBigInteger('catagory_id')->nullable();
$table->unsignedBigInteger('language_id')->nullable();
$table->timestamps();
});
Schema::table('bookmaps', function($table) {
$table->foreign('book_id')->references('id')->on('book')->onDelete('cascade');
$table->foreign('subject_id')->references('id')->on('subject')->onDelete('cascade');
$table->foreign('grade_id')->references('id')->on('grade')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('author')->onDelete('cascade');
$table->foreign('catagory_id')->references('id')->on('catagories')->onDelete('cascade');
$table->foreign('language_id')->references('id')->on('language')->onDelete('cascade');
});
}
Migrations for other related tables are:
public function up()
{
Schema::create('book', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('discription')->nullable();
$table->string('book_file')->nullable();
$table->string('card_image')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->timestamps();
});
Schema::table('book', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
The table created by above migration is only mapped by foreign key rest of the below migrations are not mapped.
public function up()
{
Schema::create('subject', function (Blueprint $table) {
$table->id();
$table->string('subject_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('grade', function (Blueprint $table) {
$table->id();
$table->string('grade_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('author', function (Blueprint $table) {
$table->id();
$table->string('author_name');
$table->string('author_discription')->nullable();
$table->timestamps();
});
}
public function up()
{
Schema::create('catagories', function (Blueprint $table) {
$table->id();
$table->string('catagories_name');
$table->timestamps();
});
}
public function up()
{
Schema::create('language', function (Blueprint $table) {
$table->id();
$table->string('language_name');
$table->timestamps();
});
}
What could be the problem?
You have to create the subject migration before the bookmarks. When Laravel tries to migrate the bookmarks table, there is a foreign key linked to the subject table that it does not find.

Laravel 7 migration :Syntax error or access violation: 1068 Multiple primary key defined

when I try to do php artisan migrate it gives me this error
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1068 Multiple primary key defined (SQL: alter table users add primary key users_user_id_primary(user_id))
I m pretty new to laravel tho
here is my migrations :
User migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id('user_id')->primary();
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
});
}
Post migration
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id('post_id');
$table->timestamps();
$table->string('content');
$table->foreignId('user_id')->constrained();
$table->primary(['post_id', 'user_id']);
});
}
Categories migration
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id('id_cat')->primary();
$table->string('nom');
});
}
Marques migration
public function up()
{
Schema::create('marques', function (Blueprint $table) {
$table->id('marque_id');
$table->foreignId('cat_id')->constrained();
$table->string('designation');
$table->primary(['marque_id','cat_id']);
});
}
UserMarques migration
public function up()
{
Schema::create('user_marques', function (Blueprint $table) {
$table->foreignId('cat_id')->constrained();
$table->foreignId('marque_id')->constrained();
$table->timestamps();
$table->primary(['marque_id','user_id']);
});
}
Use $table->increments('user_id'); instead of $table->id('user_id')->primary();
and with others.
BTW, i prefer only id instead of with model name, so in this case i prefer this:
$table->increments('id');
Update for Laravel 7.x
Laravel 7.x migration comes with id method
public function id($column = 'id')
{
return $this->bigIncrements($column);
}
so in this case: you can use id like this
$table->id();
PS: increments key comes with primary key

How to just update a table and add new line with Laravel?

in the database table as below;
public function up()
{
Schema::create('current_adresses', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('current_name',50)->nullable();
$table->string('current_surname',50)->nullable();
$table->string('telephone',25)->nullable();
$table->timestamps();
});
}
I want to do as below;
public function up()
{
Schema::create('current_adresses', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('current_name',50)->nullable();
$table->string('current_surname',50)->nullable();
$table->string('gsm',25)->nullable();
$table->string('telephone',25)->nullable();
$table->timestamps();
});
}
how can I update the new column(gsm column) without refreshing(php artisan migrate:refresh)
Add new migration-
public function up()
{
Schema::table('current_adresses', function($table) {
$table->string('gsm',25)->nullable();
});
}
public function down()
{
Schema::table('current_adresses', function($table) {
$table->dropColumn('gsm');
});
}
See this link for better understanding.

Laravel migration foreign keys references to a primary key with two columns?

I am using Laravel 5.3 and MySQL.
How can I add in Laravel foreign keys references to a primary key with two columns?
Below are my migration scripts (under the database/migrations/ directory):
primary key with two columns
public function up()
{
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
and in another,
public function up()
{
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->foreign('c')->references(['a', 'b'])->on('X')->onDelete('cascade');
$table->timestamps();
});
}
However, it doesn't work so: how can I achieve that?
Use Schema::table() instead of Schema::create() when adding foreign key constraints to your database.
Below, snippets illustrating the fix:
// File name: 2016_09_28_create_x_table.php
public function up()
{
// Create table X
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
// Create table Y
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('c')->unsigned();
$table->timestamps();
});
// Add Foreign key
Schema::table('Y', function (Blueprint $table) {
$table->foreign('c')->references('a')->on('X')->onDelete('cascade');
});
}
Remember unsigned() should be applied on the c.
This is the only way that I found to simulate composite keys and FK pointing to composite keys working in Laravel 5.3 - I miss a more compacted solution in Laravel.
Anyway, here is my code
// File name: 2016_09_28_create_x_table.php
public function up()
{
// Create table X
Schema::create('X', function (Blueprint $table) {
$table->increments('j');
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->unique(['a', 'b']);
$table->timestamps();
});
}
// File name: 2016_09_28_create_y_with_foreignkey_table.php
public function up()
{
// Create table Y
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('c')->unsigned();
$table->timestamps();
});
// Add Foreign key
Schema::table('Y', function (Blueprint $table) {
$table->foreign('c')->references('j')->on('X')->onDelete('cascade');
});
}
Foreign key should be set for 2 columns. foreign() should get also array
public function up()
{
Schema::create('X', function (Blueprint $table) {
$table->integer('a')->unsigned();
$table->integer('b')->unsigned();
$table->primary(['a', 'b']);
$table->timestamps();
});
}
public function up()
{
Schema::create('Y', function (Blueprint $table) {
$table->increments('k');
$table->integer('a');
$table->integer('b');
$table->foreign(['a', 'b'])->references(['a', 'b'])->on('X')->onDelete('cascade');
$table->timestamps();
});
}`

Categories