Trying to create a table with an uid field that auto-increments and is the primary key, but keep getting an error

I’m working on an .install file for my module in Drupal 7. I want to create two tables, one called tls_connect_floormap_images and one called tls_connect_floormap_coords. I’m not having any trouble with the _images table. But when I try to make the uid field of the _coords table a unique key that auto-increments, I get an error that says:

Fatal error: Unsupported operand types in C:Program Files (x86)ZendApache2htdocsTLSConnect3Appincludesdatabasemysqlschema.inc on line 85

I get this error on install, When I Save Configuration I get the WSOD with the error message. When I put in the url for /admin/modules there is an error that says Notice: Undefined variable: schema in floormap_install() (line 37 of C:Program Files (x86)ZendApache2htdocsTLSConnect3AppsitesallmodulescustomTLSConnectPackagefloormapfloormap.install). I see that the module is installed though, and when I check for my tables in phpMyAdmin they are there, uid auto_increments but is not a unique or primary key.

Here is my code:

function floormap_schema(){     $schema['floormap_images'] = array (             'description'   => 'Floor map images.',             'fields'        => array (                 'image'     => array('type'     => 'blob',      'not null'  => TRUE,),                 'name'      => array( 'type'        => 'varchar',       'length'        => 30,      'not null'  => TRUE,),                 'active'    => array('type'     => 'int',           'unsigned'  => TRUE,    'not null'  => TRUE,),             ),             'unique keys'   => array('name' => array('name'),             ),         );       $schema['floormap_coords'] = array (         'description'   => 'Floor map images.',         'fields'        => array (             'uid'       => array( 'type'    => 'serial',    'unsigned'  => TRUE,    'not null'  => TRUE,),             'floormap'  => array( 'type'    => 'text',  'not null'  => TRUE,),             'item_type' => array( 'type'    => 'text',  'not null'  => TRUE,),             'pos_a_x'   => array( 'type'    => 'int',       'unsigned'  => TRUE,    'not null'  => TRUE,),             'pos_a_y'   => array( 'type'    => 'int',       'unsiged'   => TRUE,    'not null'  => TRUE,),             'pos_b_x'   => array( 'type'    => 'int',       'unsigned'  => TRUE,    'not null'  => TRUE,),             'pos_b_y'   => array( 'type'    => 'int',       'unsiged'   => TRUE,    'not null'  => TRUE,),             'pos_c_x'   => array( 'type'    => 'int',       'unsigned'  => TRUE,    'not null'  => TRUE,),             'pos_c_y'   => array( 'type'    => 'int',       'unsiged'   => TRUE,'   not null'   => TRUE,),             'pos_d_x'   => array('type' => 'int',       'unsigned'  => TRUE,    'not null'  => TRUE,),             'pos_d_y'   => array('type' => 'int',       'unsiged'   => TRUE,    'not null'  => TRUE,),         ),         'unique keys'   => array('uid' => array('uid'),),     );       return $schema; } function floormap_install() {      //Create table to store floor map images if it doesn't already exist     if(!db_table_exists('tls_connect_floormap_images')){         db_create_table('tls_connect_floormap_images',$schema['floormap_images']);     }     //Create table to store floor map coordinates if it doesn't already exist     if(!db_table_exists('tls_connect_floormap_coords')){         db_create_table('tls_connect_floormap_coords',$schema['floormap_coords']);     } } 

I only get the error when I have

'unique keys' => array('uid' => array('uid'),), 

in the $schema[‘floormap_coords’] array. It works in the [‘floormap_images’] array though, not sure why its not working for coords. The only difference is that the ‘uid’ field is serial datatype and name is varchar.

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Trying to create a table with an uid field that auto-increments and is the primary key, but keep getting an error

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.