This is mine
public function down()
{
Schema::table('customer_plans', function (Blueprint $table) {
$table->dropUnique('customer_plans_code_unique');
$table->string('code',255)->change();
$table->string('name',255)->change();
});
}
I do this as laravel.com but i got this error
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'customer_plans_code_unique'; check that column/
key exists
Anyone knows this error, please tell me!
Thanks for reading!
Try removing $table->dropUnique('customer_plans_code_unique'); from your code.
Without seeing the rest of the code I can't tell if having that line is important or not, but it's definitely the reason you're seeing the error.
Show me your up() function and I can give you more feedback.
Related
After a composer update in my Laravel project all my tests fails. My Laravel version is 9.24.0
I use PHPUnit tests in a Docker container on Alpine Linux image (php:8.1-fpm-alpine).
pdo_sqlite and sqlite3 php modules are both enabled and loaded (php -m said).
When I run docker-compose run --rm phpunit I get this result for all my tests:
Domain\Customer\Tests\Address\AddressModelTest::myTestName
Illuminate\Database\QueryException: SQLSTATE[HY000]:
General error: 1 near "0": syntax error (SQL: CREATE TABLE ...
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:759
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:719
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:545
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:109
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:394
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:241
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:338
/var/www/html/database/migrations/2021_11_17_154600_alter_table_modify_content_column_type.php:18
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:482
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:400
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:409
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:210
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/View/Components/Task.php:36
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:740
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:210
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:175
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:118
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:87
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:616
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:99
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php:41
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:651
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:139
/var/www/html/vendor/symfony/console/Command/Command.php:308
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:124
/var/www/html/vendor/symfony/console/Application.php:998
/var/www/html/vendor/symfony/console/Application.php:299
/var/www/html/vendor/symfony/console/Application.php:171
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:102
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:194
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:263
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:286
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:465
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:80
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:47
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:22
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:123
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:92
/var/www/html/tests/MultitenancyTestCase.php:34
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 near "0": syntax error
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:538
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:752
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:719
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:545
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php:109
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:394
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:241
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:338
/var/www/html/database/migrations/2021_11_17_154600_alter_table_modify_content_column_type.php:18
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:482
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:400
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:409
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:210
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/View/Components/Task.php:36
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:740
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:210
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:175
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:118
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:87
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Migrations/Migrator.php:616
/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/MigrateCommand.php:99
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php:41
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:37
/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php:651
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:139
/var/www/html/vendor/symfony/console/Command/Command.php:308
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php:124
/var/www/html/vendor/symfony/console/Application.php:998
/var/www/html/vendor/symfony/console/Application.php:299
/var/www/html/vendor/symfony/console/Application.php:171
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:102
/var/www/html/vendor/laravel/framework/src/Illuminate/Console/Application.php:194
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:263
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:286
/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/PendingCommand.php:465
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:80
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:47
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/RefreshDatabase.php:22
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:123
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:92
/var/www/html/tests/MultitenancyTestCase.php:34
UPDATE:
The content of 2021_11_17_154600_alter_table_modify_content_column_type.php file is:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('menu_contents', function (Blueprint $table) {
$table->mediumText('content')->change();
}); # this is the 18. line
I tried to downgrade laravel/framework:9.3.0, but itsn't helped.
Any idea how can I fix this?
See here for recent issue in doctrine/dbal https://github.com/doctrine/dbal/issues/5584 rolling back to 3.3.7 resolved my issues.
I must create new table in octobercms project and I followed documentation and added new migration file inside plugin update file I have create_currency_rates_table.php file and it has this codes
<?php namespace RainLab\User\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreateCurrencyRateTable extends Migration
{
public function up()
{
Schema::create('currency_rates', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('currency');
});
}
public function down()
{
Schema::drop('currency_rate');
}
}
when I used php artisan october:up it is not detecting new migration. How can I create new table?Can anyone help me?
You also need to update plugins\<author>\<plugin_name>\updates\version.yaml this file and add your file name there as well.
So, in your case, you added a file like create_currency_rates_table.php then you need to add details of your file in version.yaml
for ex:
1.0.1: First version of Demo
1.0.2:
- Description About what is this update about?
- create_currency_rates_table.php
now when you next time just login to backend this table will be created automatically.
if any doubt please comment.
I tried to change id field from Integer auto increment to string but after I run migrate, the structure of MYSQL's not change.
This is my code:
Schema::table('table', function ($table) {
$table->dropPrimary();
$table->string('id', 50)->change()->primary();
});
Please help me, thank you so much.
You should write this. Hopefully this will solve your problem
Schema::table('table', function ($table) {
$table->dropPrimary('id');
$table->string('id', 50)->change()->primary();
});
Also you should check if doctrine/dbal is installed successfully
It's been 2 days now I'm trying to fix a problem and am still failing. Thank you to whoever can help on this matter.
The error
I keep getting this message:
Doctrine\DBAL\Driver\PDOException : SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans3 does not exist
There is a complex process during an action, and I need to check several tables. As it goes through several functions, the process generates nested transactions.
It's not the first time I use transactions, and even nested transactions, everything worked so far. Only this function triggers the error.
Even if I remove the DB::beginTransaction() and its friends from the function, I stil have the error. Except "trans3" becomes "trans2".
I reduced the function code to the simplest in order to trigger the error:
public function doStuff(Stuff $stuff, User $user)
{
$thing = Thing::where('user_id', $user->id)
->where('other_id', $stuff->id)
->get()
->first();
$thing->delete();
return false;
}
What I already tried
As seen in the simplifed code, removing DB::beginTransaction and such
doesn't prevent the error.
I also tried to set a save operation after changing a property from true to false, with $thing->fill(['property' => false]); $thing->save();, same error. If the property isn't changed from what is in database, no error (for instance $thing->fill(['property' => true]); $thing->save(); triggers no error, because no update triggered).
I tried using DB::raw('raw SQL'), no difference.
I tried DB::delete('RAW SQL');, no difference.
Technical environment
Laravel 5.5
mysql Ver 15.1 Distrib 10.0.36-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
PHP 7.0.32-0ubuntu0.16.04.1
The only solution I have in mind right now is removing all transactions in all parent code. Even if I ignore the danger it represents, the time required for this is out of scope, so I'd like to make sure there is no other alternative.
Does anyone have any idea, lead, anything ?
Thank you for your time.
This answer may come way too late but I was having what seems to be the same problem.
It was only when I decided to try removing the transaction that I found there was an underlying error. One of the inserts was causing an integrity constraint violation error which my tests where reporting as SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist.
Once I resolved the underlying error I restored my transaction and everything worked.
In my case, it works in my local environment, but it fails in CI.
Finally, I found the point.
Local MySQL
sql_mode: NO_ZERO_IN_DATE, NO_ZERO_DATE, NO_ENGINE_SUBSTITUTION
CI MySQL
sql_mode: STRICT_TRANS_TABLES, ERROR_FOR_DIVISION_BY_ZERO, NO_AUTO_CREATE_USER, NO_ENGINE_SUBSTITUTION
I found that when I created the table, I didn't give the default value of the varchar type, and I forgot to write it when I assigned the value.
Therefore, in my case, the main reason is mysql strict mode.
May be helpful to you.
(By the way, the framework I am using is not laravel. )
This is not a solution, just a workaround, and not really satisfying. But still, it helped me prevent several problems.
I created a TransactionService
class TransactionService
{
/**
* #return bool
* #throws \Exception
*/
public function beginTransaction() {
if(DB::transactionLevel() === 0) {
DB::beginTransaction();
return true;
}
return false;
}
/**
* #param bool $ongoingTransaction
*/
public function commit($ongoingTransaction) {
if($ongoingTransaction === true) {
DB::commit();
}
}
/**
* #param bool $ongoingTransaction
*/
public function rollBack($ongoingTransaction) {
if($ongoingTransaction === true) {
DB::rollBack();
}
}
}
And I use DB transactions only through it:
$sqlOngoingTransaction = Transaction::beginTransaction();
try {
// do stuff
} catch (\Exception $e) {
Transaction::rollBack($sqlOngoingTransaction);
throw($e);
}
Transaction::commit($sqlOngoingTransaction);
I had this error in a symfony 4 project. It was due to a conflict with dama/doctrine-test-bundle
Error disappeared after removing extension in phpunit config :
I have two models with one-to-many connection. Let it be, for example, User ans UserComment models. When I'm calling User::destroy method, I also want to destroy each UserComment instance with user_id field equal to id of destroyed User. I may simply overload User::destroy method, but I wonder if there is better ways to achieve this.
Yes, the best way (or more efficient) is to do it through migrations. Let's have a little example with User and UserComment. Leave user migration as is. For UserComment do:
Schema::create('usercomments', function(Blueprint $table){
$table->integer('user_id')->unsigned();
....
$table->foreign('user_id', 'fk_user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade'); //not needed in your case.
});
Ok, I haven't used this for a long time, so you may check the syntax online. But that's the gist of it.
When you delete a user, all his comments will be delete as well automatically, by your DB.
Goodluck mate!
I would use the database for this (as suggested above by EddyTheDove), but IF you don't want to use the database for this... you can add something to the deleting event of the User model.
EXAMPLE:
protected static function boot() {
parent::boot();
static::deleting(function($user) {
$user->user_comments()->delete();
});
}