public function normalizeTable(Table $table)
    {
        if ($table->getPrimaryKey()) {
            //search if there is already a UNIQUE constraint over the primary keys
            $pkUniqueExist = false;
            foreach ($table->getUnices() as $unique) {
                $coversAllPrimaryKeys = true;
                foreach ($unique->getColumns() as $columnName) {
                    if (!$table->getColumn($columnName)->isPrimaryKey()) {
                        $coversAllPrimaryKeys = false;
                        break;
                    }
                }
                if ($coversAllPrimaryKeys) {
                    //there's already a unique constraint with the composite pk
                    $pkUniqueExist = true;
                    break;
                }
            }

            //there is none, let's create it
            if (!$pkUniqueExist) {
                $unique = new Unique();
                foreach ($table->getPrimaryKey() as $pk) {
                    $unique->addColumn($pk);
                }
                $table->addUnique($unique);
            }

            if ($table->hasAutoIncrementPrimaryKey()) {
                foreach ($table->getPrimaryKey() as $pk) {
                    //no pk can be NULL, as usual
                    $pk->setNotNull(true);
                    //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too.
                    if (!$pk->isAutoIncrement()) {
                        //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys.
                        $pk->setPrimaryKey(false);
                    }
                }
            }
        }

        parent::normalizeTable($table);
    }
public function normalizeTable(Table $table)
    {
        if ($table->getPrimaryKey()) {
            //search if there is already a UNIQUE constraint over the primary keys
            $pkUniqueExist = false;
            foreach ($table->getUnices() as $unique) {
                $coversAllPrimaryKeys = true;
                foreach ($unique->getColumns() as $columnName) {
                    if (!$table->getColumn($columnName)->isPrimaryKey()) {
                        $coversAllPrimaryKeys = false;
                        break;
                    }
                }
                if ($coversAllPrimaryKeys) {
                    //there's already a unique constraint with the composite pk
                    $pkUniqueExist = true;
                    break;
                }
            }

            //there is none, let's create it
            if (!$pkUniqueExist) {
                $unique = new Unique();
                foreach ($table->getPrimaryKey() as $pk) {
                    $unique->addColumn($pk);
                }
                $table->addUnique($unique);
            }

            if ($table->hasAutoIncrementPrimaryKey()) {
                foreach ($table->getPrimaryKey() as $pk) {
                    //no pk can be NULL, as usual
                    $pk->setNotNull(true);
                    //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too.
                    if (!$pk->isAutoIncrement()) {
                        //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys.
                        $pk->setPrimaryKey(false);
                    }
                }
            }
        }

        parent::normalizeTable($table);
    }
public function normalizeTable ( Table $ table ) { if ( $ table -> getPrimaryKey ( ) ) { //search if there is already a UNIQUE constraint over the primary keys $ pkUniqueExist = false ; foreach ( $ table -> getUnices ( ) as $ unique ) { $ coversAllPrimaryKeys = true ; foreach ( $ unique -> getColumns ( ) as $ columnName ) { if ( ! $ table -> getColumn ( $ columnName ) -> isPrimaryKey ( ) ) { $ coversAllPrimaryKeys = false ; break ; } } if ( $ coversAllPrimaryKeys ) { //there's already a unique constraint with the composite pk $ pkUniqueExist = true ; break ; } } //there is none, let's create it if ( ! $ pkUniqueExist ) { $ unique = new Unique ( ) ; foreach ( $ table -> getPrimaryKey ( ) as $ pk ) { $ unique -> addColumn ( $ pk ) ; } $ table -> addUnique ( $ unique ) ; } if ( $ table -> hasAutoIncrementPrimaryKey ( ) ) { foreach ( $ table -> getPrimaryKey ( ) as $ pk ) { //no pk can be NULL, as usual $ pk -> setNotNull ( true ) ; //in SQLite the column with the AUTOINCREMENT MUST be a primary key, too. if ( ! $ pk -> isAutoIncrement ( ) ) { //for all other sub keys we remove it, since we create a UNIQUE constraint over all primary keys. $ pk -> setPrimaryKey ( false ) ; } } } } parent :: normalizeTable ( $ table ) ; }
Unfortunately, SQLite does not support composite pks where one is AUTOINCREMENT,
so we have to flag both as NOT NULL and create in either way a UNIQUE constraint over pks since
those UNIQUE is otherwise automatically created by the sqlite engine.

@param Table $table
Unfortunately SQLite does not support composite pks where one is AUTOINCREMENT so we have to flag both as NOT NULL and create in either way a UNIQUE constraint over pks since those UNIQUE is otherwise automatically created by the sqlite engine .