I am working in a project where a table has a foreign key called tax_id, the problem is that in some point another migration was created to change the tax_id to nullable.
Schema::table('products', function (Blueprint $table) {
$table->unsignedInteger('tax_id')->nullable()->change();
});
I realize this after I wrote other migrations and tried to run them, it throws Column 'tax_id' cannot be NOT NULL, it seems that in the "project" that migration was run, so I can't just delete the file, how can I run my migrations without the error? I have tried to remove the foreign key, but nothing worked.
I'll assume you're working with MySQL InnoDB because you did not specify the database type/engine.
You could try different approaches:
1) If yuo're in hurry, disable foreign keys, do the update, enable the foreign key. You should then correct the problem of course.
2) Add set tax_id to '' where tax_id is null and then do the migration
3) Document yourself about the database engine and implement a correct solution
Giacomo
Related
I have a SQLite database with the following migration defined:
Schema::create("views", function(Blueprint $table){
$table->integer("match_id");
$table->string("pov");
$table->string("teams");
$table->text("moves");
$table->bigIncrements("id");
$table->date("created_at");
$table->date("updated_at");
});
Note: id is the primary key of the table, match_id should just be treated as any other column.
This all works just fine, but I want match_id to be nullable. I tried changing the $table -> integer("match_id") to the following:
$table->integer("match_id")->nullable()->change();
//Also tried $table->integer("match_id")->nullable(true)->change();
I also ran 'composer require doctrine/dbal', which I saw online
When I try running php artisan migrate, it says there is nothing to migrate. And when I run php artisan migrate:fresh, for some reason the match_id column just doesn't get added to the database at all, even though every other column works just fine. Any reason this might be?
Since migrate:fresh is an option (as pointed by #xenooooo), just edit your original migration to:
Schema::create("views", function(Blueprint $table) {
$table->bigIncrements("id");
$table->integer("match_id")->nullable();
$table->string("pov");
$table->string("teams");
$table->text("moves");
$table->timestamps();
});
This should fix the problem. With migrate:fresh you don't need a second migration just to change, since it will drop all tables and create everything again. Also I've changed the order of the primary key (just in case of this be the problem of migrate:fresh not creating the match_id column), and changed the timestamps format. In Laravel you don't need to create manually the timestamps, "$table->timestamps()" do it for you :)
By using this
$table->foreignId('match_id')->nullable();
$table->integer('match_id')->nullable();
In Laravel 7
you can make "match_id" (nullable & integer) or foreign-key by using this code
$table->foreignId('test_id')->nullable();
$table->integer('tint_id')->nullable();
As i used it in picture
here is code
and here is my db
I am building an eCommerce site and I am running into an issue when migrating my orders table. I am getting the following error:
SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table `orders` add constraint `orders_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade)
The schema that sets up the foreign key for this migration looks like:
// set up foreign key on users
$table->integer('user_id')->unsigned()->nullable(); // will be null if guest
$table->foreign('user_id')
->references('id')->on('users')
->onUpdate('cascade')
->onDelete('set null');
I figured out that the users table schema sets up the id like so:
$table->id();
And the orders table schema sets up the id like this:
$table->increments('id');
So I changed the users migration so that it is the same as the orders migration i.e both are like:
$table->increments('id');
But when I run php artisan migrate, I get this back:
➜ MobileMastery_V2 git:(master) ✗ php artisan migrate --path=/database/migrations/2014_10_12_000000_create_users_table.php
Nothing to migrate.
As you can see above I tried declaring the path to the migration I wanted to see if that would work but it didn't. How can I get this single file to migrate? Thanks
Laravel migrations are intended to keep track of the changes you make on your database over time. Laravel creates a table called migrations that keeps track of the migrations if it already ran or not. So in this case you can create a new migration that only makes that change, or if your database has no data on it yet you can drop the database schema, make your modifications on the migrations and run it again.
And about the primary key and foreign keys, I recommend you to read this part of Laravel documentation.
You should create migration for this change and run php artisan migrate.
I have migration in Postgres and SQLite, and I am facing problem with SQLite database. Same migrations are for 2 different databases. Postgres migration is OK, but not SQlite. I added migration for altering table and adding new field.
public function up()
{
Schema::table('retailer_products_photo_fix', function ($table) {
$table->integer('currency_id')->unsigned();
$table
->foreign('currency_id')
->references('id')
->on('currency')
->onUpdate('cascade')
->onDelete('cascade')
;
$table->dropColumn('currency');
});
}
But the following error was thrown:
General error: 1 Cannot add a NOT NULL column with default value NULL
When I try to add nullable() field isn't created and when I add default value 0 or 1 I got constraint error because in related table I have rows 1 and 2. How can this be solved?
Quoting from this answer by Daniel Vassallo:
SQLite doesn't support the ADD CONSTRAINT variant of the ALTER TABLE command
(The source that answer uses is here)
So attempting to alter your table by adding a foreign key constraint isn't going to work. You will need to:
Rename the table
Recreate the table with an updated structure (including your FK constraint)
Populate new table with existing data, providing values for new fields
Drop your old table
You can swap #1 and #2 so that you create a temporary version of your table, and then drop the old one / rename the new one at the very end, but that's up to you. It may be safer to go that route instead in case something goes wrong as you won't have your old table in a renamed state at that point.
I have created two migrations with the Jeffrey Way generators for Laravel 5. Namely: php artisan make:migrate:schema create_roles_table --schema='name:string, description:text' and php artisan make:migrate:pivot user role.
Now, my MySQL database is defaulted to InnoDB, which I've read should be. I also moved the foreign key setup to Schema::table() instead of Schema::create('role_user'). There shouldn't be anything wrong with my migrations, yet they error out with: General error: 1215 Cannot add foreign key constraint (SQL: alter table role_user add constraint role_user_user_id_foreign foreign key (user_id) references user (id) on delete cascade).
Schema::create('role_user', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('role_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->primary(['role_id', 'user_id']);
});
Schema::table('role_user', function(Blueprint $table){
$table->foreign('user_id')->references('id')->on('user')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('role')->onDelete('cascade');
});
I have called $table->engine = 'InnoDB'; in every migration. Also have I set all id columns to unsigned, even though they were like that already. I am clueless, who helps me out..
Check to see if you have already migrated the tables users and roles. Otherwise you will not be able to create a foreign key. Check your migration order as well. If you are migrating this one before the users and roles it will fail.
Well, apparently this was a huge typo. user instead of users, for example. I had hoped Laravel would get that right, but unfortunately Laravel and I as well didn't.
This might be related to your SQL engine, check your engine (mariadb, mysql, ...)
And then read the documentation from your engine regarding foreign keys.
Quick solution
Remove the onDelete('cascade') and run the migration.
You can add it manually from phpMyAdmin later on
When i create Laravel5 migration as follows it adds "linkdesc" column as a primary key. when i read the documentation of laravel5 migration it does not mentioned that $table->text('description'); this gives a primary key in database.
is there any way to prevent such automatically added primary keys in laravel5 ?
Also is there any other migration functionalities that give this kind of unwanted primary keys ?
my migration is as follows
Schema::create('articles', function (Blueprint $table) {
$table->primary(['pemail', 'linkid']);
$table->bigInteger('linkid');
$table->string('pemail');
$table->string('linkname');
$table->string('linkurl');
$table->integer('linorder');
$table->text('linkdesc')->nullable();
});
I think you misread the info you get in PhpMyAdmin. You have primary word inactive without possibility to click on it in the row for linkdesc column and you thought Laravel creates primary key for this column.
However the truth is quite different - it's inactive not because Laravel created automatically primary key for this field but because it's a text field and texts fields cannot be primary keys.
You can verify it when you click in PhpMyAdmin on Indexes - it should be below the table - there you will see there is no primary key on linkdesc column.