1

I am pulling my hair for the past half of the day trying to figure out what is wrong. Here is my hook_schema function:

<?php

function image_gallery_schema() {
  $schema = array();

  $schema['gallery'] = array(
    'description' => 'The main store for gallery entity',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key for our table of gallery',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'User id',
        'type' => 'int',
        'not null' => TRUE
      ),
      'title' => array(
        'description' => 'Title of the gallery',
        'type' => 'text',
        'not null' =>TRUE,
       ),
      'created' => array(
        'description' => 'Creation time',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE
      ),
      'changed' => array(
        'description' => 'Changed time',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE
      ),
      'gall_description' => array(
        'description' => 'Description of gallery',
        'type' => 'text',
        'not null' =>TRUE,
       ),
    ),
    'primary key' => array('id'),
  );

  $schema['photos'] = array(
    'description' => 'For storing photos',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key for photo',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'g_id' => array(
        'description' => 'Gallery id',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'full_path' => array(
        'description' => 'full photo path',
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'short_path' => array(
        'description' => 'short photo path',
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'img_description' => array(
        'description' => 'Image description',
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
      'g_id' => array('g_id'),
    ),
    'foreign keys' => array(
      'gid' => array(
        'table' => 'gallery',
        'columns' => array('g_id' => 'id'),
      ),
    ),
  );

  return $schema;
}

I checked all the brackets and everything seems to be in place. It does create gallery table, but fails to create photo table. Error I get:

Syntax error or access violation: 1064 You have an error in your SQL syntax;

I was trying to see how gallery schema is different from photos but I can't find anything. Maybe something is wrong with indexes and foreign keys? I checked multiple examples and everything seems to be the same. If someone could help me out, I would be more thank thankful.

  • Did you check that there doesn't exist a photos table. Just to be sure, and to comply with Drupal code standards, your tables should be named image_gallery_gallery and image_gallery_photos – juankvillegas Oct 6 '16 at 14:31
  • Do you get an error when you enable the module? – juankvillegas Oct 6 '16 at 14:32
  • Yes, please prefix your tables appropriately. Uninstall, and make sure the tables do not exist in the database - reenable the module, and see if the error persists. – Kevin Oct 6 '16 at 15:18
  • @juankvillegas I will update my table names accordingly, and yes, I get when I try to update my module. I already did that, every time I try to enable my module and create those tables, I drop previously created one. So the problem persists sadly – Viktor Oct 6 '16 at 16:51
2

It must be because you are adding the unsigned attribute to text fields.

| improve this answer | |
  • @Viktor, if this is not the reason, please copy and paste the error you are getting to have a clue about what might be happening. – juankvillegas Oct 6 '16 at 18:42
0

Simply remove the indexes array in photos array. It should look something like

function image_gallery_schema() {
  $schema = array();

  $schema['gallery'] = array(
    'description' => 'The main store for gallery entity',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key for our table of gallery',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'uid' => array(
        'description' => 'User id',
        'type' => 'int',
        'not null' => TRUE
      ),
      'title' => array(
        'description' => 'Title of the gallery',
        'type' => 'text',
        'not null' =>TRUE,
       ),
      'created' => array(
        'description' => 'Creation time',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE
      ),
      'changed' => array(
        'description' => 'Changed time',
        'type' => 'int',
        'default' => 0,
        'not null' => TRUE
      ),
      'gall_description' => array(
        'description' => 'Description of gallery',
        'type' => 'text',
        'not null' =>TRUE,
       ),
    ),
    'primary key' => array('id'),
  );

  $schema['photos'] = array(
    'description' => 'For storing photos',
    'fields' => array(
      'id' => array(
        'description' => 'Primary key for photo',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'g_id' => array(
        'description' => 'Gallery id',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'full_path' => array(
        'description' => 'full photo path',
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'short_path' => array(
        'description' => 'short photo path',
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'img_description' => array(
        'description' => 'Image description',
        'type' => 'text',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('id'),
    'foreign keys' => array(
      'gid' => array(
        'table' => 'gallery',
        'columns' => array('g_id' => 'id'),
      ),
    ),
  );

  return $schema;
}

Lemme know if you have any confusions.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.