Skip to main content
Tweeted twitter.com/StackDrupal/status/1073004721898696704
removed "Drupal" from the title; removed tags from the title
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

How todo I alter a database schema in Drupal 8defined from another module?

Since, hook_schema_alter() is hook_schema_alter() was removed from Drupal 8, how todo I alter any existing schema of somedefined from another module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;

But, but no new field or column gets added into my existing table. Is theirthere any other way to achieve this?

@edit

This is the code for the first module a

a.install(a.install).

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

This is the code for the second module b(b.install).

/**
 * Implements  hook_update_N()().
 */
    function b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('my_table', 'newcol', $spec);
     return $schema;
    }

How to alter database schema in Drupal 8?

Since, hook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;

But no new field or column gets added into my existing table. Is their any other way to achieve this?

@edit

module a

a.install

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

module b

/**
 * Implements  hook_update_N()().
 */
    function b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('my_table', 'newcol', $spec);
     return $schema;
    }

How do I alter a database schema defined from another module?

Since hook_schema_alter() was removed from Drupal 8, how do I alter any existing schema defined from another module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) in my .install file, but no new field or column gets added into my existing table. Is there any other way to achieve this?

This is the code for the first module (a.install).

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

This is the code for the second module (b.install).

/**
 * Implements  hook_update_N().
 */
    function b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('my_table', 'newcol', $spec);
     return $schema;
    }
added 293 characters in body
Source Link
Sugandh Khanna
  • 3.5k
  • 5
  • 40
  • 69

Since, hook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;

But no new field or column gets added into my existing table. Is their any other way to achieve this?

@edit

module a

a.install

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

module b

/**
 * Implements  hook_update_N()().
 */
    function mymodule_update_8201b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('mytable''my_table', 'newcol', $spec);
     return $schema;
    }

But no new field or column gets added into my existing table. Is their any other way to achieve this?

Since, hook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;
/**
 * Implements  hook_update_N()().
 */
function mymodule_update_8201() {
 $spec = array(
    'type' => 'varchar',
    'description' => "New Col",
    'length' => 20,
    'not null' => FALSE,
  ); 
 $schema = Database::getConnection()->schema();
 $schema->addField('mytable', 'newcol', $spec);
 return $schema;
}

But no new field or column gets added into my existing table. Is their any other way to achieve this?

Since, hook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;

But no new field or column gets added into my existing table. Is their any other way to achieve this?

@edit

module a

a.install

function a_schema() {
  $schema['my_table'] = array(
    'fields' => array(
       'type' => array(
          'description' => 'registration type',
          'type' => 'varchar',
          'length' => 255,
        ),
     ),
  );
}

module b

/**
 * Implements  hook_update_N()().
 */
    function b_update_8201() {
     $spec = array(
        'type' => 'varchar',
        'description' => "New Col",
        'length' => 20,
        'not null' => FALSE,
      ); 
     $schema = Database::getConnection()->schema();
     $schema->addField('my_table', 'newcol', $spec);
     return $schema;
    }
edited body
Source Link
ya.teck
  • 5.7k
  • 3
  • 35
  • 40

Since, hook_alter_schemahook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;
/**
 * Implements  hook_update_N()().
 */
function mymodule_update_8201() {
 $spec = array(
    'type' => 'varchar',
    'description' => "New Col",
    'length' => 20,
    'not null' => FALSE,
  ); 
 $schema = Database::getConnection()->schema();
 $schema->addField('mytable', 'newcol', $spec);
 return $schema;
}

But no new field or column gets added into my existing table. Is their any other way to achieve this?

Since, hook_alter_schema() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;
/**
 * Implements  hook_update_N()().
 */
function mymodule_update_8201() {
 $spec = array(
    'type' => 'varchar',
    'description' => "New Col",
    'length' => 20,
    'not null' => FALSE,
  ); 
 $schema = Database::getConnection()->schema();
 $schema->addField('mytable', 'newcol', $spec);
 return $schema;
}

But no new field or column gets added into my existing table. Is their any other way to achieve this?

Since, hook_schema_alter() is removed from Drupal 8, how to alter any existing schema of some module in Drupal 8?

I have used mymodule_update_8201 (hook_update_N()) function in my .install file

use Drupal\Core\Database\Database;
/**
 * Implements  hook_update_N()().
 */
function mymodule_update_8201() {
 $spec = array(
    'type' => 'varchar',
    'description' => "New Col",
    'length' => 20,
    'not null' => FALSE,
  ); 
 $schema = Database::getConnection()->schema();
 $schema->addField('mytable', 'newcol', $spec);
 return $schema;
}

But no new field or column gets added into my existing table. Is their any other way to achieve this?

deleted 13 characters in body
Source Link
Sugandh Khanna
  • 3.5k
  • 5
  • 40
  • 69
Loading
Source Link
Sugandh Khanna
  • 3.5k
  • 5
  • 40
  • 69
Loading