migrations icon indicating copy to clipboard operation
migrations copied to clipboard

BUG on transaction (PDOException: There is no active transaction)

Open ludeus opened this issue 6 years ago • 2 comments

This is a (multiple allowed):

  • [x] bug

  • [ ] enhancement

  • [ ] feature-discussion (RFC)

  • CakePHP Version: 3.6.11

  • Migrations plugin version: 2.0.0

  • Database server: Postgres 9.5.13

  • PHP Version: 7.2.9

  • Platform / OS: Ubuntu 16.04 LTS

What you did

Insert data in the up() function. Here a small example:

<?php

use Cake\ORM\TableRegistry;
use Migrations\AbstractMigration;

class CreateTest extends AbstractMigration
{
    public function up()
    {
        $table = $this->table('tests');
        $table->addColumn('name', 'string', [
            'default' => null,
            'limit' => 255,
            'null' => false,
        ]);
        $table->create();

        $table = TableRegistry::getTableLocator()->get('Tests');
        $entity = $table->newEntity(['name' => 'foo']);
        $table->save($entity);
    }
    public function down()
    {
        $this->table('tests')->drop()->save();
    }
}

Note: this code work in migrations v1.7.2

Expected Behavior

Creation of table: tests and insertion of 'foo'

Actual Behavior

Exception: There is no active transaction in [/.../vendor/cakephp/cakephp/src/Database/Driver.php, line 197]

Alternatives

This work:

$table->save($entity, ['atomic' => false]);

But this wont:

$table = TableRegistry::getTableLocator()->get('Tests');
$table->addBehavior('Acl.Acl', ['type' => 'requester']);
$entity = $table->newEntity(['name' => 'foo']);
$table->save($entity, ['atomic' => false]);

This work:

$this->getAdapter()->commitTransaction();
$table = TableRegistry::getTableLocator()->get('Tests');
$table->addBehavior('Acl.Acl', ['type' => 'requester']);
$entity = $table->newEntity(['name' => 'foo']);
$table->save($entity, ['atomic' => false]);
$this->getAdapter()->beginTransaction();

The full working example file: 20180926072527_CreateTest.php.txt

ludeus avatar Sep 26 '18 09:09 ludeus

I had similar issue when using the seed feature for postgresql. Using your workaround works. So thanks!

But I thought this was fixed by #91. So /cc @lorenzo

rchavik avatar Aug 23 '19 12:08 rchavik

Same bug is on Microsoft SQL server but there are no alternatives from above working.

SunshineNoir avatar Sep 22 '19 21:09 SunshineNoir