SQLite does not create an index - php

I am working on some database migrations in Laravel 5.4. The migrations work fine with a MySQL database, but for testing I want to use SQLite but the migration fails. Here's the code
public function up()
{
Schema::create('mapped_venues', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('upload_job_id')->nullable();
$table->string('venue')->default('');
$table->unsignedInteger('venue_id')->nullable();
$table->timestamps();
$table->index(['venue']);
});
Schema::create('mapped_teams', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('upload_job_id')->nullable();
$table->string('team')->default('');
$table->unsignedInteger('team_id')->nullable();
$table->timestamps();
$table->index(['team']);
});
}
When I run php artisan migrate the index on the mapped_teams.team column is not created, but the one on mapped_venues.venue is!!
$ sqlite3 database/database.sqlite
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.
sqlite> .indexes mapped_teams
sqlite> .indexes mapped_venues
mapped_venues_venue_index
sqlite>
I have also tried to create the indexes on a separate call
Schema::table('mapped_venues', function (Blueprint $table) {
$table->index(['venue']);
});
Schema::table('mapped_teams', function (Blueprint $table) {
$table->index(['team']);
});
But the result is the same. Interestingly though, when (by mistake) I left the creation of the index $table->index['team']) inside the call to create the table (so, I have two calls to create the index) I get the error that the index mapped_teams_team_index already exists.
I am using:
Laravel 5.4.36
Doctrine DBal 2.6.2
SQLite 3.19.3

It seems like you have small mistake when calling the index function (used it as an array instead of a function):
$table->index['venue'];
should be:
$table->index('venue');

I have actually found out that the index was indeed created. I had another migration to rename the team column to mapped_team, and I wanted to remove the index first
Schema::table('mapped_teams', function (Blueprint $table) {
$table->dropIndex(['team']);
$table->renameColumn('team', 'mapped_team');
$table->index(['mapped_team']);
}
The line where the index is dropped complained that the index mapped_teams_team_index didn't exist. I have modified my migration to not drop and recreate the index, but just rename it. The result is that the index named mapped_teams_team_index still exists but it now, correctly, indexes the mapped_team column. This works on both Mysql and SQLite.

Related

laravel, update table structure but get error table already exist when use php artisan migrate

I have table and I wanted to update on some columns, or if I wanted to add new column the problem is when I want to use php artisan migrate command gives me error table already exist, also Im using depoly file and the command inside it is php artisan migrate --force so hope this is correct or have to add any more command??
public function up()
{
Schema::create('payment_methods', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->string('paymentreference')->unique();
$table->string('payment_token');
$table->string('cardnumber'); //updated
$table->string('cardbin')->nullable();
$table->string('cardlast4');
$table->string('cardtype');
$table->string('expirymonth');
$table->string('expiryyear');
$table->string('cardholdername'); //added
$table->timestamps();
});
}
To added new or update field like profile in payment_methods.
Try
Run command:
php artisan make:migration add_profile_to_payment_methods
And in the up() method of the new migration file generated, use Schema::table() method to add the new columns or modifying the table.
public function up()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->string('profile')->nullable();
});
}
public function down()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->dropColumn('profile');
});
}
}
Then run migration to update the table using php artisan migrate
You need a new migration to modify existing table.
Create new migration:
php artisan make:migration modify_payment_methods_table
Then open the migration file and put following code in there:
public function up()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->string('cardnumber')->change();
$table->string('cardholdername');
});
}
public function down()
{
Schema::table('payment_methods', function (Blueprint $table) {
$table->integer('cardnumber')->change(); // todo: if this was not an integer then fix this to be correct type instead of integer to avoid issue in case if you will have to rollback the migration
$table->dropColumn('cardholdername');
});
}
After this run
php artisan migrate
To do this successfully you may need to install additional dependency doctrine/dbal.
You can install that easily with composer:
composer require doctrine/dbal
You have manipulated or an error has occurred in any of the migrations.
Well, now in the migrations table, there isn't a row that contains create_payment_methods_table in the migrations column.
As it does not exist, but the table to which the migration refers, if it exists in your database, it fails you, since the process is as follows:
When you refresh, Laravel reads the migrations table, and executes
each migration file in order, first executing the down or deletion of
the table.
After executing that step in all migrations, go through the UP. When
the down of that table does not exist, when arriving at its demo
file, the up finds that it already exists. And that's why it fails
you.,
The solution is to delete manually the referenced table and rerun the migration

Laravel adding extra field in table results in General error: 2057

My problem is quite simple, but I can't find any answer for this specific issue.
The thing is, I have this migration in Laravel:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
//$table->string('guid');
$table->boolean('verified')->default('0');
$table->integer('tribe_id')->length(10)->unsigned()->default('1');
$table->boolean('admin')->default('0');
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->foreign('tribe_id')->references('id')->on('tribes')->onDelete('cascade');
});
}
As you can see I have one field commented. But when I remove the // and run the migration (on a clean DB) and when I try to perform login it returns this:
lluminate \ Database \ QueryException (HY000)
SQLSTATE[HY000]: General error: 2057 A stored procedure returning result sets of different size was called. This is not supported by libmysql (SQL: select * from `users` where `email` = email#email.com limit 1)
The major part of this auth code is generated by default Laravel artisan make:auth command.
The thing is, I already have some extra fields and it does work, but for some reason when I add the 'guid' new one (or another one, it doesn't matter the name) it breaks something I missing...
EDIT: It has something to do with MySQL database. I changed this from Shared Hosting MariaDB server to Google Cloud (5.7) and it's working...
EDIT2: Solved it by creating another database with another name (recreating with the same name didn't work) and it started working again...
Schema::table('users', function(Blueprint $table) {
$table->foreign('tribe_id')->references('id')->on('tribes')->onDelete('cascade');
});
try to put Blueprint in table like this...
or you can put the line $table->foreign()... inside the create method..

laravel - update database

I created an app in Laravel. In the beginning, I made a migration with the following content:
public function up()
{
Schema::create('kundens', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->unsignedInteger('user_id');
$table->string('vorname');
$table->string('nachname');
$table->string('strasse');
$table->integer('plz');
$table->string('wohnort');
$table->string('mail');
$table->integer('telefon');
$table->string('geburtsdatum');
});
}
No I want to add some tables like kaufpreis or "modernisierung". I added them under the other tables but when I save the file and write in the terminal I get the error:
nothing to migrate.
So now how can I add some tables for more information?
You should create a new migration for kaufpreis and modernisierung.
The main migration for kundens did already run (see migrations table).
php artisan migrate:fresh is also an option, if you are developing locally.
Don't do this when you work with other people / production as it will erase the tables and create new ones (data will be lost)

Laravel error while trying to migrate

I want to add another column to my database table users. I made a new migration named Modify_users_tablewhich has the code to add the column. There is nothing wrong with the source code because I used it before, but it gives me an error that something is wrong with the table roles? How can I solve this? Here is the error and the code that is related to the error
Here is the migration Roles:
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->string('role_name')->length(55)->unique();
$table->increments('id');
$table->timestamps();
});
}
You've got a number of problems. Your main problem is that you aren't adding a column to a table. Your script is instead trying to create a new table, as seen in this line:
Schema::create('roles', function (Blueprint $table) {
You said you are trying to add a column to the users table, but you aren't even working with the user's table: you are instead creating a table called roles which already exists. The simplest solution is to do what Dhaval suggests: ditch the migration you are added, update the migration that creates the user tables, nuke and start over.
The "right" answer is to create a new migration and put in the instructions to add the column to the users table as well as instructions on removing it. Then you can just do a simple migration instead of nuking your database. That would look something like this:
public function up()
{
Schema::table('users', function ( Blueprint $table ) {
$table->string('last_name')->after( 'first_name' )->default( '' );
});
}
public function down()
{
Schema::table( 'users', function ( Blueprint $table ) {
$table->dropColumn( ['last_name'] );
});
}
You need to run
php artisan migrate:rollback
if that also fails just go in and drop all the tables which you may have to do as it seems your migration table is messed up or your roles table when you ran a previous rollback did not drop the table.
or you can use migrate:reset command will roll back all of your application's migrations:
php artisan migrate:reset
I had the same issue and I tried something like this and it works perfect for me. My Laravel version is 5.5.
public function up()
{
Schema::connection('mysql2')->create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('image');
$table->timestamps();
});
}
public function down()
{
Schema::connection('mysql2')->dropIfExists('images');
}
I had to connections in my project one is mysql and other is mysql2 becuase my project is for database with multiple connections.
So my solution is that to try specifying the connection to your database in the function.
By default connection is mysql if you havent specifed it in the function, Example is given below. Hope this helps!
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email',60)->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
And so when you migrate using php artisan it doesn't show errors,
remove the table from the database ( I did it manually the first time I had this issue)
and make this change in the create_users_table migration as such
$table->string('email', 60)->unique();
and this worked for me..
Hope this helps you as well

Add extra table using Laravel Migration

I am using Laravel 5.2. I have created basic tables using migration method. But now I need to add another extra table. After creating my schema, I have given php artisan migrate command. But it shows error like base table or view already exists Table:Users. I know why this happen. The migration command trying to recreate the table which already have. But I need to add another extra table in Laravel via Migration. I have gone through this https://laravel.com/docs/5.2/migrations But I can't get any solution.
If you want to create another table, just create new migration and run it.
If you're trying to add columns into existinng table, use Schema::table instad of Schema::create.
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
}
Schema::table('articles', function (Blueprint $table) {
$table->string('description');
}
If you already executed php artisan migrate then next time it will give you error saying "Table already exists.".
So if you want to execute only a particular migration then either you can temporarily move all migration's php file which are executed, out of database/migrations folder and then execute
php artisan migrate
or
you can execute migration from tinker i.e. first execute php artisan tinker and then execute content of up method from the migration but without parameter type Blueprint.
Eg.
If following is your migration up method content
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
});
then you will have to execute
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
});

Categories