I want to write test for my application, but i getting error when run the migration;
My Migration file
Schema::table('bill_payments', function (Blueprint $table) {
$table->dropColumn('attachment');
// $table->getColumns(); return empty array
// $table->dropColumn(['attachment']); I tried this
});
dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false
// and this is not working because return false.
if(Schema::hasColumn('bill_payments', 'attachment'))
{
Schema::table('bill_payments', function (Blueprint $table) {
$table->dropColumn('attachment');
});
}
Also i add doctrine/dbal 2.5.13
i running tests using mysql, but slowly.
[Solved]
Wow! i using prefix for tables. i deleted this and now it's work.
One thing you need to know about migrations is that they run untill they crash or succeed, following your example:
Schema::table('bill_payments', function (Blueprint $table) {
$table->dropColumn('attachment');
});
//It will drop the column and stop here. When you run the migration again, it will output your error because the column no longer exists.
dd(Schema::hasColumn('bill_payments', 'attachment')); // Return false
What you should have in your migration code is having the reverse operations in the Down() method. Meaning you run the migration, it applies the Up() and when you rollback, it reverts correctly. That error is really what it means, it means that when it reaches an operation relating table bill_payments and column attachment, it recognizes that attachment doesn't exist.
Edit:
There is something related to SQlite in the documentation:
"Dropping or modifying multiple columns within a single migration while using a SQLite database is not supported."
Related
I am new to PHP and laravel platform,need your help to resolve PDOException on run the migration task to alter table column type from number to string.
...
public function up()
{
Schema::table('BuildTable', function (Blueprint $table) {
$table->string('snapshot_id')->change();
});
}
....
getting PDOException on run the migration task
Doctrine\DBAL\Driver\PDOException::("SQLSTATE[0A000]: Feature not supported: 7 ERROR: unimplemented: type conversion from INT8 to VARCHAR(255) requires overwriting existing values which is not yet implemented
HINT: You have attempted to use a feature that is not yet implemented.
Existing table structure was created using
Schema::create(
$this->tablename,
function (Blueprint $table) {
$table->increments('id');
$table->integer('account_id')->unsigned();
$table->integer('snapshot_id')->unsigned();
$table->timestamps(6);
$table->softDeletes('deleted_at', 6)->default(null);
}
);
The existing table already have data in snapshot_id
Php version is 7.3.20 running on linux mint OS , Database - cockroachDB
This feature will be experimentally available in CockroachDB v20.2, which is getting released later this year.
If you want, you can test out with an alpha version (v20.2.0-alpha.2) which has this functionality. See the release notes. To use it, you need to set the following session variable: SET enable_experimental_alter_column_type_general = true;
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
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)
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.
I've inserted a column with name im_useless to my table earlier which I do not need anymore.
This is my schema (filename: 2017_02_27_120313_units.php):
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('im_useless');
$table->timestamps();
});
}
Now I try to remove it, so I used this code inside the down() function:
public function down()
{
Schema::dropColumn('im_useless');
}
New Schema:
public function up()
{
Schema::create('units', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
}
Now I have to rollback and then migrate again. I try to rollback only that specific migration file, by executing php artisan help migrate:rollback I found out that there is a --path option.
So I tried to rollback that specific migration like this:
php artisan migrate:rollback --path=2017_02_27_120313_units.php
But I get Nothing to rollback
How can I drop that specific column without having to rollback any other migrations?
UPDATE:
I think I have to change the path like this:
php artisan migrate:rollback --path=database/migrations/2017_02_27_120313_units.php
...since my php shell was opened in the project root folder?
However I still get Nothing to rollback
I also tried php artisan migrate --path=2017_02_27_120313_units.php
and php artisan migrate --path=database/migrate/2017_02_27_120313_units.php
...and get Nothing to migrate
UPDATE 2
I think I have messed up my migrations table, because I removed the code inside the down() function and the table was never deleted.
https://stackoverflow.com/a/26077506/4684797
The rollback function is meant to give you the possibility to revert to the version you had right before you migrated, in case something goes wrong when you deploy. If you want to drop a specific column that you don't need anymore, you should treat that as a new migration and drop the column in the up() method.
If your laravel version is >= 5.3 you could simply add the migrations path like packages does.
https://laravel.com/docs/5.3/packages#migrations
Just put this code in your AppServiceProvider boot method:
$this->loadMigrationsFrom(__DIR__.'/path/to/migrations');
Also if you want to include subfolders recursively you can try with "/path/to/migrations/**/*" but I'm not sure if this will work in older laravel versions.
--path param is like a filter. It will work on the latest migrations that rollback will act. You can use path reference starting from datatabase/migrations. For example:
php artisan migrate:rollback --path=database/migrations/2022_10_03_193316_create_something.php
It will work if in your database, you have something like 2022_10_03_193316_create_something.php on migration column plus greatest batch value.
Tested on Laravel 6
Run composer dump-autoload and try again