What is causing InstallSchema Error Warning: array_key_exists() expects parameter 2 to be array, string given in

magento.stackexchange https://magento.stackexchange.com/questions/164311

Question

I'm trying to create a custom table in my module but I keep getting this error:

Warning: array_key_exists() expects parameter 2 to be array, string given in 

I've been able to step through the code and it seems to break when this is executed:

$newTable = $setup->getConnection()->newTable($installer->getTable('orders'))->addColumn(
    'id', Table::TYPE_INTEGER, null, [
    'identity' => true,
    'unsigned' => true,
    'nullable' => false,
    'primary' => true
], ' Id'

)->addColumn(
    'orderid', Table::TYPE_TEXT,
    255, [
    'nullable' => false
], 'sku'

)->addColumn(
    'last_updated_date', Table::TYPE_DATETIME, null, [
    'nullable' => false
], 'last_updated_date'
)->addColumn(
    'total_price', Table::TYPE_INTEGER, null, [
    'nullable' => false
], 'price'
)->addColumn(
    'order_state', Table::TYPE_TEXT, 255, [
    'nullable' => false
], 'order_state'
)->addColumn(
    'fields', Table::TYPE_TEXT, 2048, [
    'nullable' => false
], 'fields'
)->addColumn(
    'order', Table::TYPE_TEXT, 255, [
    'nullable' => false
], 'o_id'
)->addColumn(
    'comment', Table::TYPE_TEXT, 255, [
    'nullable' => false
], 'comment'
)->addColumn(
    'read', Table::TYPE_SMALLINT, 1, [
    'nullable' => false,
    'default' => 0
], 'read'
)->addColumn(
    'in_stock', Table::TYPE_SMALLINT, null, [
    'nullable' => false,
    'default' => 0
], 'in_stock'

)->addColumn(
    'product_title', Table::TYPE_TEXT, 255, [
    'nullable', false,
], 'title'

)->addColumn(
    'qty', Table::TYPE_NUMERIC, null, [

], 'qty'

);

Can anyone tell what I'm doing wrong here???

This is where the exception occurs

  if (array_key_exists('default', $options)) {
        $default = $options['default'];
    }

vendor/magento/framework/DB/Ddl/Table.php on line 352 
Was it helpful?

Solution

Take a look: vendor/magento/framework/DB/Ddl/Table.php::addColumn(), we can see the arguments of addColumn method:

 public function addColumn($name, $type, $size = null, $options = [], $comment = null)
  • string $name the column name - 1st argument.
  • string $type the column data type - 2nd argument.
  • string | int | array $size the column length - 3rd argument.
  • array $options array of additional options - 4th argument.
  • string $comment column description - 5th argument.

In your case, you should check each column again carefully. I saw you have a wrong column product_title:

->addColumn(
    'product_title', Table::TYPE_TEXT, 255, [
    'nullable', false,
], 'title'

The 4th argument was wrong. It should be:

->addColumn(
        'product_title',
        Table::TYPE_TEXT,
        255,
        ['nullable'=> false],
        'title'
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top