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
Related
I cant figure out what the problem is, the 2 tables are not connecting for some reason, I read many articles and tried many things still not working.
I want to link post and category tables together, so when I can display the category chosen in the post made.
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('name');
$table->text('description');
$table->integer('category_id');
$table->integer('price');
$table->integer('currency_id');
});
}
Category
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->bigInteger('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
});
}
This is the error I get:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table
'categories' already exists (SQL: create table categories (id
bigint unsigned not null auto_increment primary key, created_at
timestamp null, updated_at timestamp null, name varchar(255) not
null, post_id bigint unsigned not null) default character set
utf8mb4 collate 'utf8mb4_unicode_ci')
Try refreshing you database entirely using the migrate:refresh artisan command.
php artisan migrate:refresh --seed
It may be that a database migration ran and failed before it could register in the migrations table of your database.
Issues: (so far)
1) As per above, a migrate:refresh sorts out the original error
2) $table->bigInteger('post_id')->unsigned(); will not work as posts.id is an integer and not a bigInteger.
Solution:
Change your post_id definition to
$table->integer('post_id')->unsigned();
It says the categories table already exists. So what you have to do is, If you are in dev env, you can delete the table and try it or you can do like the below command.
Run artisan like this, it will drop all tables and migrate fresher.
php artisan migrate:fresh
This worked for me.
The answer is very simple, you have to change $table->increments('id') in posts table to $table->id(), because the foreign key must refer to a primary key as the message says.
Here are some tips for you
You have to use bigIncrements in the posts table instead integer because the length of integer is 4-byte but the bigIncrements is 8-byte and this may cause a problem in the future
You may love to use this line
$table->foreignId('post_id')->constrained();
instead
$table->bigInteger('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
for simplicity
Though, this is a past issue, but I found a very good and simple solution to this issue;
Check your category and post migration file
Check your migration files and check the public function up and public function down if the tables_names are correct and then check on your http://localhost/phpmyadmin/index.php and then go to your database
Checking migrations table
Inside your database, check for the migrations table, It looks like this
Picture of some rows inside a migrations table, you will find out that those two tables(post and category) are not inside
Copy Names of Migrations Files
Go into your laravel-app/database/migrations folder or IDE to that folder named migrations, then copy the name of both migrations files
Create new Migration rows and paste the copied names
Back to your localhost database migrations table, create new rows for the two copied names and paste the names accordingly numerically
AND
THEN RUN THE MIGRATION AGAIN. IT IS DONE
php artisan migrate
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
I am following all the steps for adding new column votes to the user table from laravel, still there isn't in the database? Please tell me where is my mistake?
Firstly
php artisan make:migration add_votes_to_users_table --table=users
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('votes');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});
}
php artisan migrate
enter image description here
Error from cmd-
In Connection.php line 647:
SQLSTATE[42S01]
And
In Connection.php line 449:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already
exists
I am suspecting that the reason you are getting this errors can come from 2 sides.
1) you created users table yourself without run any migration
2) a migration was interrupted.
If the above does not work try:
php artisan migrate:reset
to rollback all your changes or if you want to move back 1 step at a time you can try:
php artisan migrate:rollback --step=1
Last if you are sure you can start over and you are ok with that you can run:
php artisan migrate:fresh
This will drop all the tables (check the database just to be sure as well and drop yourself any remaining even though i don't think anything will be left, never happened to me) and then run
php artisan migrate
all over again
Check your data base. the users table already exit or not. if you create manually. you get this type of error. if users table ther you can remove manually from data base.
Thanks all of you for your suggestions, Sasa Blagojevic you are right about one thing my database was created in wrong way. Actually main problem arise when I created the database first time that time SQLSTATE[42000] appeared for user table. That why when updating the table previous error was still showing. After solving it there wasn't error now.
I'm using laravel 5.2 and I usually update my database according to project requirements, so I'd like to do it without losing database records.
I don't mean how to seed my database.. I mean when my database is live and I want to update it throw laravel migration.
I was going throw Laravel Documentation but I found nothing, so I hope to find somebody help me
Since you already have data in your tables then instead of rolling back your migrations (which will cause existing data losses) you can create new migration files to update your tables. Suppose you have a table users with columns name, email, password. You stored data in that table. Then you realized that you also do need to add a new column named mobile_no to your users table. To do this you need to create a new migration file. Command will be:
php artisan make:migration add_mobile_no_columns_to_users_table --table=users
This way a new migration file will be created. Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.
Please note that when you add a new column for a table while already there are data, you have to set a default value for new column or make it nullable type.. otherwise you will endup with error
Make sure that when you are adding new column in your table, that column should be nullable, and should not be unique. Otherwise you will face error. Because when a new column is created it will be empty(not unique). In that condition you have to rollback the migration.
Make a new migration with
php artisan make:migration change_body_to_nullable_in_reviews_table --table=reviews
where you put this
public function up()
{
Schema::table('reviews', function (Blueprint $table) {
$table->text('body')->nullable()->change();
});
}
public function down()
{
Schema::table('reviews', function (Blueprint $table) {
$table->text('body')->nullable(false)->change();
});
}
And then run PHP artisan migrate
I created a new migration with php artisan make:migration --table=my_table and proceed to create a new column to an existing table. Said column is supposed to be a unique index, but I forgot to make it nullable, for that reason, php artisan migrate failed, as multiple rows would have a duplicate entry (which would be an empty string '')
Up to this point, the logical way to go is to add nullable to the column and re-run migrations. But since the new column already exist on the table (the migration failed when adding the unique key, but the column creation was successful) I have to drop it first.
My first thought was to run php artisan migrate:rollback but it executed a previous migration, not the one I just created.
Up to this point, what is the right way to do this? should I log into database cli and write query manually or laravel provide a way to handle this via php artisan?
I do think you can modify the original migration for your table and add the nullable, but you need to do this on the old migration. As you noticed, a new migration will just apply a patch to an existing table.
After modifying the migration you can rollback and forward again. Of course you need to be careful with your data (in case you need them check Database Seeding.
You can also use php artisan migrate:refresh this will delete all tables and data (so be really careful), but it should recreate your table with the new structure.
Try this:
php artisan migrate:rollback --table=table_name_here