Drupal table creation error [closed] - drupal

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am trying to create a table in Drupal database using hook_schema function but I am getting the PDO exception error which I could not resolve. It would be great if you could provide me an solution to my issue
PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'DEFAULT '' COMMENT
'first name of the user', `lname` VARCHAR DEFAULT '' COMMENT' at line 3:
CREATE TABLE {mage_user} ( `uid` INT unsigned NOT NULL auto_increment COMMENT
'The foreign identifier for a mage user.', `fname` VARCHAR DEFAULT ''
COMMENT 'first name of the user', `lname` VARCHAR DEFAULT '' COMMENT
'last name of the user', `address1` VARCHAR(255) DEFAULT '' COMMENT
'adress line 1', `address2` VARCHAR(255) DEFAULT '' COMMENT 'adress line 2',
`postcode` VARCHAR(7) DEFAULT '' COMMENT 'postcode', `gender` VARCHAR(6)
DEFAULT '' COMMENT 'gender' ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8
COMMENT 'The base table for mage user.'; Array ( ) in db_create_table()
(line 2688 of E:\Server\drupal\includes\database\database.inc).
I am using
Apache/2.2.22 (Win32) PHP/5.3.10
MySQL client version: mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $
MySQL server version 5.5.21
and my schema definition is
function mageacc_schema() {
$schema['mageuser'] = array(
'description' => 'The base table for mage user.',
'fields' => array(
'uid' => array(
'description' => 'The foreign identifier for a mage user.',
'type' => 'serial',
'not null' => TRUE,
),
'fname' => array(
'description' => 'first name of the user',
'type' => 'varchar',
'lenght' => 255,
'default' => '',
),
'lname' => array(
'description' => 'last name of the user',
'type' => 'varchar',
'lenght' => 255,
'default' => '',
),
'address1' => array(
'description' => 'adress line 1',
'type' => 'varchar',
'length' => 255,
'default' => '',
),
'address2' => array(
'description' => 'adress line 2',
'type' => 'varchar',
'length' => 255,
'default' => '',
),
'postcode' => array(
'description' => 'postcode',
'type' => 'varchar',
'length' => 7,
'default' => '',
),
'gender'=>array(
'description' => 'gender',
'type' => 'varchar',
'length' => 6,
'default' => '',
),
),
);
return $schema;
}
Thanks in Advance

I spotted a few typos with the 'length' property. Try fixing those first in fname and lname to see if it works.

Related

Mysql Syntax Error in Drupal 7

I am trying to define relationship between tables using foreign keys
in drupal 7. I used hook_schema and hook_update function to define and
update the schema. I am getting following errors.
$schema['relationship] = array(
'description' => 'The table for employee organisation relationship',
'fields' => array(
'rid' => array(
'description' => 'The primary Identifier for a Relationship.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => 'The primary Identifier for User/Employee',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
),
'oid' => array(
'description' => 'The department Identifier of employee employed',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
),
'indexes' => array(
'uid' => array('uid'),
'oid' => array('oid'),
),
'foreign keys' => array(
'uid' => array(
'table' => 'users',
'columns' => array('uid' => 'uid')
),
'oid' => array(
'table' => 'organization',
'columns' => array('oid' => 'oid')
),
),
'primary key' => array('rid'),
)
);
Failed: PDOException: SQLSTATE[42000]: Syntax error or access
violation: 1064 You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near 'DEFAULT NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8
COMMENT 'The table for' at line 5: CREATE TABLE
{organisation} ( id INT unsigned NOT NULL
auto_increment COMMENT 'The primary Identifier for a Relationship.',
uid INT unsigned NULL DEFAULT 0 COMMENT 'The primary Identifier for
User/Employee', oid INT unsigned NULL DEFAULT 0 COMMENT 'The
department Identifier of employee employed', primary key DEFAULT
NULL ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'The table
for employee organisation relationship'; Array ( ) in
db_create_table() (line 2717 of
C:\xampp\htdocs\tutumaudit\includes\database\database.inc).
Well I sorted it out myself. I put those keys under the fields array.
That was damn stupid and i needed 2 hours to find this out. The final
code is below:
$schema['relationship] = array(
'description' => 'The table for employee organisation relationship',
'fields' => array(
'rid' => array(
'description' => 'The primary Identifier for a Relationship.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => 'The primary Identifier for User/Employee',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
),
'oid' => array(
'description' => 'The department Identifier of employee employed',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => 0,
),
),
'indexes' => array(
'uid' => array('uid'),
'oid' => array('oid'),
),
'foreign keys' => array(
'uid' => array(
'table' => 'users',
'columns' => array('uid' => 'uid')
),
'oid' => array(
'table' => 'organization',
'columns' => array('oid' => 'oid')
),
),
'primary key' => array('rid'),
);

Drupal 6 schema not installing all tables

So this has been driving me crazy for 2 days, I have a module I've written that uses 3 DB tables, 2 of them install perfectly, and this is the third one:
$schema['tags_twistal'] = array(
'description' => t('Taxonomy for videos (tags)'),
'fields' => array(
'vid' => array(
'description' => t('The video ID'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
'tag' => array(
'description' => t('The tag name'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
),
),
'primary key' => array('tag','vid'),
);
All I can think is that it has something to do with the primary key that I set, I've also tried:
'unique keys' => array(
'tag_vid' => array('tag', 'vid'),
),
'primary key' => array('tag_vid'),
Any ideas? I'm about to pull my hair out!
I'm having a similar issue in Drupal 7.
The tables of mine that install correctly are those which have a matching entry in the array returned by hook_node_info().
It looks as though Drupal will not create any tables that are not referenced in hook_node_info(), even if they are explicitly enumerated in hook_scheme(). I can't find this documented anywhere, but it matches my experience, and is a pain in the butt.

Drupal 7: Make custom content translatable

I'm creating a custom module which enables me to add Countries to a custom table in the database. I'll do later more with it, but as I got stuck at the beginning, I can't go on.
First my piece of code:
function partners_schema()
{
$schema['partners_country'] = array(
'description' => 'TODO: please describe this table!',
'fields' => array(
'id' => array(
'description' => 'auto inc id of country',
'type' => 'serial',
'not null' => true,
),
'name' => array(
'description' => 'name of country',
'type' => 'varchar',
'length' => '255',
'not null' => true,
'translatable' => true,
),
'needDistributor' => array(
'description' => 'is a distributor needed',
'type' => 'int',
'size' => 'tiny',
'not null' => true,
),
),
'primary key' => array('id'),
);
return $schema;
}
The code above generates my Database table. After searching I found that I can add 'translatable' => true to my schema, so Drupal knows that this field is translatable content.
I've added a form, to insert data into that schema, but now I got stuck. What do I have to do, that the user is able to translate the column name?
Thanks for your help.
Have a look at the accepted answer on this post it should help clear a few things up.

ER-Diagram : How to manage sessions ? (parallel drupal <-> symfony2)

I am building my app and have been annoyed concerning sessions storage. I am a newbie about sessions. I am working with Symfony2 and MySQL, and as Symfony is storage-agnostic, I am searching into the Drupal 7 diagram to find a good model.
So I wondered a couple of things :
How to manage sessions in an Entity-Relationship diagram ?
In drupal 7 diagram, what do sessions->fields mean ?
Sessions -> uid (int(10)) -> OK
Sessions -> sid (varchar(128)) ?
Sessions -> ssid (varchar(128)) ?
Sessions -> hostname (varchar(128)) -> OK
Sessions -> timestamp (int(11)) -> guessing date of connection
Sessions -> cache (int(11)) -> Why only an integer ?
Sessions -> session (longblob) -> What do you put inside ?
As I imagined my own diagram, I had 2 tables :
Session that stored the sessionId, cookie and establishing date
User_Session, association between Sessions and Users, which stores IP address and DateInit.
Why not storing one session by Cookie and storing also each time the user connects ?
If someone could help me understand and help me find the true entity-relationship model...
Description of the session table from Drupal 7 is given in its system_schema() function:
$schema['sessions'] = array(
'description' => "Drupal's session handlers read and write into the sessions table. Each record represents a user session, either anonymous or authenticated.",
'fields' => array(
'uid' => array(
'description' => 'The {users}.uid corresponding to a session, or 0 for anonymous user.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'sid' => array(
'description' => "A session ID. The value is generated by Drupal's session handlers.",
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
),
'ssid' => array(
'description' => "Secure session ID. The value is generated by Drupal's session handlers.",
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'hostname' => array(
'description' => 'The IP address that last used this session ID (sid).',
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
),
'timestamp' => array(
'description' => 'The Unix timestamp when this session last requested a page. Old records are purged by PHP automatically.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'cache' => array(
'description' => "The time of this user's last post. This is used when the site has specified a minimum_cache_lifetime. See cache_get().",
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'session' => array(
'description' => 'The serialized contents of $_SESSION, an array of name/value pairs that persists across page requests by this session ID. Drupal loads $_SESSION from here at the start of each request and saves it at the end.',
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
),
),
'primary key' => array(
'sid',
'ssid',
),
'indexes' => array(
'timestamp' => array('timestamp'),
'uid' => array('uid'),
'ssid' => array('ssid'),
),
'foreign keys' => array(
'session_user' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
),
);
But this is not an E-R model. Also, it heavily depends on Drupal own session handling function.

Drupal: "Schema module" installed and new error in status report

I've installed schema module to import database entries into my drupal CCK fields. Now in the statuus report I get the following error message. Can I just ignore, or is something serious I should fix ?
Database schema
Inconsistent The
Schema comparison report shows:
42 modules with matching tables
41 extra tables
2 warnings
3 module with mis-matching tables
More precisely:
Mismatch (4)
Tables for which the schema and database are different.
user
users
timezone_name: unexpected column in database
image_fupload
fupload_previewlist
column uid - difference on: length
declared: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'length' => '10', 'not null' => TRUE, 'default' => 0)
actual: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'not null' => TRUE, 'default' => 0)
column nid - difference on: length
declared: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'length' => '10', 'not null' => TRUE, 'default' => 0)
actual: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'not null' => TRUE, 'default' => 0)
column fid - differences on: not null, length
declared: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'length' => '11', 'not null' => FALSE, 'default' => 0)
actual: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'not null' => TRUE, 'default' => 0)
column created - difference on: length
declared: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'length' => '11', 'not null' => TRUE, 'default' => 0)
actual: array('description' => t('TODO: please describe this field!'), 'type' => 'int', 'not null' => TRUE, 'default' => 0)
Maybe
Make sure that you've run update.php and that all database changes by modules are up to date.
You can sometimes run into trouble with a staging and live site if you copy databases or modules between them and fail to keep database changes in sync. Not much you can do there but manually rectify.
If your site is fully updated and there are no dev/staging/live discrepancies then it's unlikely that there's anything you should do other than report the issue to the module developer.

Resources