cannot create taxonomy terms using hook_enable solved! i want to create 1 taxonomy vocabulary and 1 taxonomy term using drupal's hook_enable. but when i enable the custom module, it create only the taxonomy vocabulary. And if i disable and enable the module, the taxonomy term will be created without problem. the code is as below:

 $sql = "SELECT vid FROM {taxonomy_vocabulary} WHERE machine_name='article_type'";


$vid = db_query($sql)-&gt;fetchField();
    if (!$vid) {
      drupal_set_message(t("Created article type vocabulary"));
      // No vocabulary exists, we'll create one.
      $vocab = (object) array(
          'name' =&gt; 'article type',
          'description' =&gt; t('Automatically created by the custom_block module'),
          'machine_name' =&gt; 'article_type',
          'multiple' =&gt; '0',
          'required' =&gt; '0',
          'hierarchy' =&gt; '1',
          'relations' =&gt; '0',
          'module' =&gt; 'custom_block',
      );
      taxonomy_vocabulary_save($vocab);
      $vid = $vocab-&gt;vid

}

  $terms = (object) array (
          'name' =&gt; 'News',
          'vid' =&gt; $vid,
          'vocabulary_machine_name' =&gt; 'article_type',);

 $term_name = $terms -&gt; name;


  $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid and name = :name", array(
    ':vid' =&gt; $vid,
    ':name' =&gt; $term_name,
  ))-&gt;fetchField();


if ($vid &amp;&amp; !$tid) {

    taxonomy_term_save($terms);
}

}


PS:i've figured it out! it's a stupid mistake. i did not get the vid after saving the vocabulary to the database. i just need to add $vid = $vocab->vid after taxonomy_term_save.
$vid remains FALSE throughout the entire first run. You need to set it after the vocabulary object is saved:

...
taxonomy_vocabulary_save($vocab);
// Add this next line
$vid = $vocab-&gt;vid;