Cake\Database\Schema\SqliteSchema::constraintSql PHP Method

constraintSql() public method

Note integer primary keys will return ''. This is intentional as Sqlite requires that integer primary keys be defined in the column definition.
public constraintSql ( Table $table, $name )
$table Table
    public function constraintSql(Table $table, $name)
    {
        $data = $table->constraint($name);
        if ($data['type'] === Table::CONSTRAINT_PRIMARY && count($data['columns']) === 1 && $table->column($data['columns'][0])['type'] === 'integer') {
            return '';
        }
        $clause = '';
        if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
            $type = 'PRIMARY KEY';
        }
        if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
            $type = 'UNIQUE';
        }
        if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
            $type = 'FOREIGN KEY';
            $clause = sprintf(' REFERENCES %s (%s) ON UPDATE %s ON DELETE %s', $this->_driver->quoteIdentifier($data['references'][0]), $this->_convertConstraintColumns($data['references'][1]), $this->_foreignOnClause($data['update']), $this->_foreignOnClause($data['delete']));
        }
        $columns = array_map([$this->_driver, 'quoteIdentifier'], $data['columns']);
        return sprintf('CONSTRAINT %s %s (%s)%s', $this->_driver->quoteIdentifier($name), $type, implode(', ', $columns), $clause);
    }

Usage Example

 /**
  * Test the constraintSql method.
  *
  * @dataProvider constraintSqlProvider
  */
 public function testConstraintSql($name, $data, $expected)
 {
     $driver = $this->_getMockedDriver();
     $schema = new SqliteSchema($driver);
     $table = (new Table('articles'))->addColumn('title', ['type' => 'string', 'length' => 255])->addColumn('author_id', ['type' => 'integer'])->addConstraint($name, $data);
     $this->assertEquals($expected, $schema->constraintSql($table, $name));
 }