In my Larave-8, I created a table using this migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
How do I alter the table through migration and add these:
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
Thanks
create another migration like to alter existing table
public function up()
{
Schema::table('profiles', function (Blueprint $table) {
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
public function down() {
Schema::table('profiles', function (Blueprint $table) {
$table->dropForeign('nationality_id');
$table->dropForeign('country_id');
$table->dropForeign('state_id');
$table->dropForeign('city_id');
});
}
Related
So, I want to add a column ('Semester_TA') and change the property of this column ('kode_mk') to be unique value, so I created this migration:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
Schema::table('lr2', function(Blueprint $table){
$table->dropForeign(['id']);
});
}
public function down()
{
//
}
I also want to drop/delete this foreign key ('id') that I created as it sometimes give a constraint error in my project. My question is what function down should I write to reverse the update above that i'm about to do in case of migration fails or error?
this is the table "mk" and "lr2" migrations that i use right now:
Schema::create('mk', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('kode_mk');
$table->string('mk');
$table->integer('sks');
$table->integer('semester');
$table->integer('available_seats');
$table->timestamps();
});
Schema::create('lr2', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('k_mk');
$table->string('mk');
$table->string('angkatan');
$table->integer('request_seats');
$table->boolean('status_request');
$table->timestamps();
$table->foreign('id')->references('id')->on('mk');
});
First, you have to create a new migration:
php artisan make:migration Add_column_change_the_property
Then in that migration file up(), try:
public function up()
{
Schema::table('mk', function(Blueprint $table){
$table->string('Semester_TA')->after('sks');
$table->string('kode_mk')->unique();
});
}
Then in the migration file down() try:
public function down()
{
$table->dropColumn('kode_mk');
$table->foreign('id')->references('id')->on('mk');
//
}
Then migrate the file by:php artisan migrate
I want to add a foreign key to my schools table but the primary key of the provinces table is not added to the foreign key reference.
This is my Schools Table
public function up()
{
Schema::create('schools', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name");
$table->unsignedTinyInteger("province_id");
});
}
and Provinces Table
public function up()
{
Schema::create('provinces', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name",255);
});
}
and this is my Foreign key constraint
public function up()
{
Schema::table('schools', function (Blueprint $table) {
$table->foreign('province_id')->reference('id')->on('provinces');
});
}
and this is the error
enter image description here
If there is a solution tell me.
Thanks.
Try this and also check the sequence of your migrations.
public function up()
{
Schema::create('student_profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name",64);
$table->string("father_name",64);
$table->string("last_name",64);
$table->string("grand_father_name",64);
$table->string("graduation_year",4)->unique();
$table->enum("gender",['0','1']);
$table->unsignedSmallInteger('school_id')->nullable();
$table->unsignedTinyInteger("province_id")->nullable();
$table->foreign('school_id')->reference('id')->on('schools')-
>onDelete('set null');
$table->foreign('province_id')->reference('id')->on('provinces')->onDelete('set null');
});
}
public function up()
{
Schema::table('schools', function (Blueprint $table) {
$table->unsignedBigInteger('province_id')->nullable();
$table->foreign('province_id')->reference('id')->on('provinces')
->onDelete('set null');
});
}
I have a problem when I rollback my boards. It is as follows:
SQLSTATE[HY000]: General error: 3730 Cannot drop table 'questionnaires' referenced by a foreign key constraint 'questions_questionnaire_id_foreign' on table 'questions'. (SQL: drop table if exists `questionnaires`)
For this I have 2 tables involved, which is Questionnaire and Question.
Question
lass CreateQuestionsTable extends Migration{
public function up(){
Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('questionnaire_id');
$table->text('description');
$table->text('iframe');
$table->text('image')->nullable();
$table->timestamps();
});
}
public function down(){
Schema::table('questions', function (Blueprint $table) {
$table->dropColumn('questionnaire_id');
$table->dropForeign('questions_questionnaire_id_foreign');
});
Schema::dropIfExists('questions');
}
}
Questionnaire
class CreateQuestionnairesTable extends Migration{
public function up(){
Schema::create('questionnaires', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->string('description');
$table->timestamps();
});
}
public function down(){
Schema::dropIfExists('questionnaires');
}
}
And then add the questionnaire FK to the question table. Alter table
class AddToQuestionnaireIdToQuestionsTable extends Migration{
public function up(){
Schema::table('questions', function (Blueprint $table) {
$table->foreign('questionnaire_id')->references('id')->on('questionnaires')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down(){
Schema::dropIfExists('questions');
}
}
However when I rollback I get the aforementioned error. What am I doing wrong?
You are dropping the question table in the wrong place. You should rollback only what you did roll forward.
Question: if you created a table, here you drop the table.
Class CreateQuestionsTable extends Migration
{
public function up()
{
Schema::create('questions', function (Blueprint $table)
{
$table->id();
$table->unsignedBigInteger('questionnaire_id');
$table->text('description');
$table->text('iframe');
$table->text('image')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('questions');
}
}
ForeigKey: if you created a column and/or foreign key, drop then here.
class AddToQuestionnaireIdToQuestionsTable extends Migration
{
public function up()
{
Schema::table('questions', function (Blueprint $table)
{
$table->foreign('questionnaire_id')
->references('id')
->on('questionnaires')
->onDelete('cascade')
->onUpdate('cascade');
});
}
public function down()
{
Schema::table('questions', function (Blueprint $table)
{
$table->dropForeign('questions_questionnaire_id_foreign');
$table->dropColumn('questionnaire_id');
});
}
}
If you want to delete a table and get a foreign key error, do as the code below in SQL😂
set foreign_key_checks=0;
drop table `yourSchema`.`yourTable`;
migration file
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->string('short_description');
$table->unsignedInteger('media_id')->nullable();
$table->foreign('media_id')->references('id')->on('medias');
$table->unsignedInteger('creator_id');
$table->foreign('creator_id')->references('id')->on('users');
$table->boolean('hide')->default(0);
$table->timestamps();
});
}
after hide column i want to add 'privacy column'
Either add it to the migration file or create another migration like this :
Schema::table('posts', function (Blueprint $table) {
$table->string('privacy')->after('hide');
});
I am trying to make two tables relationship in laravel. But i can't understand why i'm getting error "errno: 150 "Foreign key constraint is incorrectly formed"". please check my migration code below.
items table
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('name');
$table->decimal('price',5,2);
$table->integer('count_left')->default(0);
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});
}
categories
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}