drupal db schema error - php

In a module I'm building for Drupal 7 I'm trying to create a DB schema for the installing and uninstalling process. When enabling the module (and thus installing it) I get an error and my table is not created.
Can anybody see where it's going wrong?
<?php
/**
* Implementation of hook_schema().
*/
function wind_and_waves_schema() {
$schema['wind_and_waves'] = array(
'description' => 'Caches wind and waves data',
'fields' => array(
'id' => array(
'description' => 'The unique identifier for this item.',
'type' => 'serial',
'disp-width' => '11',
'unsigned' => TRUE,
),
'creation' => array(
'description' => 'Moment the data has been loaded',
'mysql_type' => 'datetime',
'disp-width' => '11',
),
'load_time' => array(
'description' => 'Moment the wind data has been loaded by rijkswaterstaat',
'mysql_type' => 'datetime',
'disp-width' => '11',
),
'hoek_wind_snelheid' => array(
'description' => 'Windsnelheid op hoek',
'type' => 'varchar',
'disp-width' => '11',
),
'hoek_wind_stoot' => array(
'description' => 'Windstoot op hoek',
'type' => 'varchar',
'disp-width' => '11',
),
'hoek_wind_richting' => array(
'description' => 'Windrichting op hoek',
'type' => 'varchar',
'disp-width' => '11',
),
'hoek_golf_hoogte' => array(
'description' => 'golfhoogte op hoek',
'type' => 'varchar',
'disp-width' => '11',
),
'ijmuiden_wind_snelheid' => array(
'description' => 'Windsnelheid op ijmuiden',
'type' => 'varchar',
'disp-width' => '11',
),
'ijmuiden_wind_stoot' => array(
'description' => 'Windstoot op ijmuiden',
'type' => 'varchar',
'disp-width' => '11',
),
'ijmuiden_wind_richting' => array(
'description' => 'Windrichting op ijmuiden',
'type' => 'varchar',
'disp-width' => '11',
),
'ijmuiden_golf_hoogte' => array(
'description' => 'golfhoogte op ijmuiden',
'type' => 'varchar',
'disp-width' => '11',
),
'golf_periode' => array(
'description' => 'golfperiode',
'type' => 'varchar',
'disp-width' => '11',
),
'watertemp' => array(
'description' => 'watertemperatuur',
'type' => 'varchar',
'disp-width' => '11',
),
'luchttemp' => array(
'description' => 'luchttemperatuur',
'type' => 'varchar',
'disp-width' => '11',
),
),
'primary key' => array('id'),
);
return $schema;
}
?>
The error I get:
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 COMMENT 'Windsnelheid op hoek', hoek_wind_stoot VARCHAR DEFAULT ' at line 4: CREATE TABLE {wind_and_waves} ( id INT unsigned auto_increment DEFAULT NULL COMMENT 'The unique identifier for this item.', creation DATETIME DEFAULT NULL COMMENT 'Moment the data has been loaded', hoek_wind_snelheid VARCHAR DEFAULT NULL COMMENT 'Windsnelheid op hoek', hoek_wind_stoot VARCHAR DEFAULT NULL COMMENT 'Windstoot op hoek', hoek_wind_richting VARCHAR DEFAULT NULL COMMENT 'Windrichting op hoek', hoek_golf_hoogte VARCHAR DEFAULT NULL COMMENT 'golfhoogte op hoek', ijmuiden_wind_snelheid VARCHAR DEFAULT NULL COMMENT 'Windsnelheid op ijmuiden', ijmuiden_wind_stoot VARCHAR DEFAULT NULL COMMENT 'Windstoot op ijmuiden', ijmuiden_wind_richting VARCHAR DEFAULT NULL COMMENT 'Windrichting op ijmuiden', ijmuiden_golf_hoogte VARCHAR DEFAULT NULL COMMENT 'golfhoogte op ijmuiden', golf_periode VARCHAR DEFAULT NULL COMMENT 'golfperiode', watertemp VARCHAR DEFAULT NULL COMMENT 'watertemperatuur', luchttemp VARCHAR DEFAULT NULL COMMENT 'luchttemperatuur', PRIMARY KEY (id) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COMMENT 'Caches wind and waves data'; Array ( ) in db_create_table() (line 2657 of /home/sitede01/domains/sitedezign.net/public_html/includes/database/database.inc).

All columns of type varchar need a length property, e.g.:
'luchttemp' => array(
'description' => 'luchttemperatuur',
'type' => 'varchar',
'length' => 11
)
disp-width is only valid for non-varchar type columns

Related

How to set Datetime as type for a column on Codeigniter migration

I am doing a migration file, and there is a field on a table wich type should be DateTime, and the default value CURRENT_TIMESTAMP. What I have until now is the next:
'date' => array(
'type' => 'DATETIME',
),
I think it is right... but I still need set the default value... definitely, what I want to do is make the translation from sql to codeigniter migration of this:
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
Thanks in advance.
Best way to Codeigniter migration:
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'first_name' => array(
'type' => 'VARCHAR',
'constraint' => '50',
'null'=>false,
),
'last_name' => array(
'type' => 'VARCHAR',
'constraint' => 50,
'null'=>false,
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => 100,
'unique' => true,
'null'=>false,
),
'mobile' => array(
'type' => 'BIGINT',
'constraint' => 15,
'null'=>true,
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => 255,
'null'=>false,
),
'image' => array(
'type' => 'VARCHAR',
'constraint' => 255,
'null'=>true,
),
'status' => array(
'type' => 'TINYINT',
'constraint' => 3,
'default'=>1,
),
'delete_status' => array(
'type' => 'TINYINT',
'constraint' => 3,
'default'=>1,
),
'created_date datetime default current_timestamp',
'updated_date datetime default current_timestamp on update current_timestamp',
));
You can try like this.Need to use timestamp.See an example below..
'created_at' => array('type' => 'timestamp')
I have found this answer in the codeigniter forum. Basically, it says that I can use created_at timestamp default current_timestampinstead of key->value array. And there are other interesant things like 'on update':
'updated_at' => array(
'type' => 'varchar',
'constraint' => 250,
'null' => true,
'on update' => 'NOW()'
)
I hope it can to help anyone on future.

how to set on update attributes in a timestamp field using dbforge class in codeigniter 3

I want to set on update attributes for getting an updated time for my application. Here is my Model Code:
public function createDatabase() {
$testdatabase = "testdatabase";
$this->dbforge->create_database($testdatabase);
$this->db->db_select($testdatabase);
$fields = array(
'id' => array(
'type' => 'INT',
'constraint' => 1,
'unsigned' => TRUE,
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'establishment_date' => array(
'type' => 'VARCHAR',
'constraint' => '50',
'null' => TRUE,
),
'package' => array(
'type' => 'VARCHAR',
'constraint' => '15',
),
'db_name' => array(
'type' => 'VARCHAR',
'constraint' => '25',
),
'update_date' => array(
'type' => 'datetime',
'on update' => 'NOW()',
),
'upgrade' => array(
'type' => 'tinyint',
'constraint' => '1',
'default' => '0',
),
'status' => array(
'type' => 'tinyint',
'constraint' => '1',
'default' => '4',
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_field("subscription_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP");
$this->dbforge->add_field("subscription_expire TIMESTAMP DEFAULT '0000-00-00 00:00:00'");
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('tms_profile');
}
Table is successfully created but on update attributes not exists on the field named update_date. i want to set on update atrributes on the field.
All you have to do is change type of update_date column like
....
'update_date' => array(
'type' => 'TIMESTAMP'
),
.....
Now it will work ON UPDATE with NOW()
use inline declaration, like here:
`update_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',

Create database table using Drupal hook_schema()

For the last few hours I have been trying to create database table in my custom module with no luck!! The documentation says that you only need to implement hook_schema in Drupal 7 and the Drupal takes care of creating the table during module installation. I just cant figure out why the table is not created. I tried installing the module through Drupal admin as well as pasting in the module folder but it still doesnt work.
Here is my function inside .install file:
/**
* #return array an array containing table's field definitions
* to be created by drupal schema api
*/
function inquiry_form_schema() {
$schema['inquiry_form'] = array(
'description' => 'Table to record inquiries submitted by staff',
'fields' => array(
'ID' => array(
'description' => 'The primary key of the table',
'type' => 'serial',
'size' => 'big',
'not null' => TRUE,
),
'sender' => array(
'description' => 'The person who submits the inquiry',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'subject' => array(
'description' => 'Title of the inquiry being submitted',
'type' => 'char',
'length' => '100',
'not null' => TRUE,
),
'department' => array(
'description' => 'The department that the inquiry is being referenced to',
'type' => 'varchar',
'length' => '100',
'not null' => TRUE,
),
'description' => array(
'description' => 'Detailed explanation of the issue the sender is experiencing',
'type' => 'text',
'not null' => TRUE,
),
// 'date' => array(
// 'description' => 'Date of the inquiry being submitted',
// 'mysql_type' => 'datetime',
// 'not null' => TRUE,
// ),
),
'primary key' => array('ID'),
);
return $schema;
}
I'm assuming your files are:
- inquiry_form.info
- inquiry_form.module
- inquiry_form.install
Is this the case?
Make sure the length is not quoted.
Should be 100 and not '100'

Creating Datetime Column in Database using TCA file

In my Typo3 extension ,I want one of my table column as 'datetime' with type as'timestamp' and default as 'CURRENT_TIMESTAMP'.
How can I create this in TCA file.
I have given my code below. But this is not creating the column with type timespamp and default value as CURRENT_TIMESTAMP.
'datetime' => array(
'exclude' => 0,
'label' => 'LLL:EXT:besi_jobs/locallang_db.xml:tx_jobs_messages.datetime',
'config' => array(
'type' => 'timestamp',
'size' => '12',
'max' => '20',
'eval' => 'datetime',
'checkbox' => '0',
'default' => 'CURRENT_TIMESTAMP'
)
),
The type must be set to 'input':
'datetime' => array(
'exclude' => 0,
'label' => 'LLL:EXT:besi_jobs/locallang_db.xml:tx_jobs_messages.datetime',
'config' => array(
'type' => 'input',
'size' => '12',
'max' => '20',
'eval' => 'datetime',
'checkbox' => '0',
'default' => time(),
)
),
NOTES
All the possible types are described in the TCA reference.
The field in the .sql file file of your extension should be int(11) unsigned NOT NULL DEFAULT '0'. The TCA definition neither creates nor alters the database. It only defines how the field is displayed in the forms.

drupal 7 custom schema error datetime

I have the following schema (generated from an existing table with the schema module (7.x-1.0-beta3) in a custom module.
function myproject_entities_schema() {
// ---------------------------------------------------------------------------------
// MESSAGE
// ---------------------------------------------------------------------------------
$schema['myproject_entity_message'] = array(
'description' => 'The base table for myproject message instances',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for a message instance',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'weekendday' => array(
'description' => 'Whether this message gets send on a weekendday(1) or on a workday(0)',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
),
'day' => array(
'description' => 'Numbered day of campaign schedule',
'type' => 'int',
'not null' => TRUE,
),
'content' => array(
'description' => 'Message content',
'type' => 'text',
'not null' => TRUE,
),
'time_to_send' => array(
'description' => 'When the message should arrive at the user',
'type' => 'datetime',
'not null' => TRUE,
),
'campaign_fk' => array(
'description' => 'the foreign key of the campaign this message belongs to',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
'user_id' => array(
'description' => 'The user id this message is generated for',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'pool_id' => array(
'description' => 'The pool node id this message was generated from',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'message_template_id' => array(
'description' => 'The node id of the message template this message was generated from',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
),
'primary key' => array('id'),
'indexes' => array(
'CAMPAIGN' => array('campaign_fk'),
'MESSAGE_TEMPLATE' => array('message_template_id'),
'POOL' => array('pool_id'),
'USER' => array('user_id'),
),
);
// ---------------------------------------------------------------------------------
// CAMPAIGN
// ---------------------------------------------------------------------------------
$schema['myproject_entity_campaign'] = array(
'description' => 'The base table for myproject campaigns instances',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for a campaign instance',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'schedule' => array(
'description' => 'The schedule of the campaign in text format.',
'type' => 'text',
'not null' => TRUE,
),
'interruptible' => array(
'description' => 'Whether this campaign is interruptible or not',
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
),
'campaign_template_id' => array(
'description' => 'Node id of campaign template this instance was created from',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'product_fk' => array(
'description' => 'Primary key of product instance this campaign belongs to',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'primary key' => array('id'),
'indexes' => array(
'CAMPAIGN_TEMPLATE' => array('campaign_template_id'),
'PRODUCT' => array('product_fk'),
),
);
// ---------------------------------------------------------------------------------
// PRODUCT
// ---------------------------------------------------------------------------------
$schema['myproject_entity_product'] = array(
'description' => 'The base table for myproject product instances',
'fields' => array(
'id' => array(
'description' => 'The primary identifier for a product instance',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'start' => array(
'description' => 'Time when the campaign started',
'type' => 'datetime',
'not null' => TRUE,
),
'current_campaign_fk' => array(
'description' => 'Foreign key of currently running campaign instance',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
'next_campaign_fk' => array(
'description' => 'Foreign key of campaign instance to run next',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'primary key' => array('id'),
'indexes' => array(
'CURRENT' => array('current_campaign_fk'),
'NEXT' => array('next_campaign_fk'),
),
);
return $schema;
}
When i try to install the module and thus create the schema, I always get the following notice (which I dont think is the main reason for failing):
Notice: Undefined index: datetime:normal in DatabaseSchema_mysql->processField() (line 200 of /Users/xxx/Repos/myproject/includes/database/mysql/schema.inc).
And the following error:
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 'NOT NULL COMMENT 'When the message should arrive at the user', `campaign_fk` IN' at line 6: CREATE TABLE {myproject_entity_message} ( `id` INT unsigned NOT NULL auto_increment COMMENT 'The primary identifier for a message instance', `weekendday` TINYINT NOT NULL DEFAULT 0 COMMENT 'Whether this message gets send on a weekendday(1) or on a workday(0)', `day` INT NOT NULL COMMENT 'Numbered day of campaign schedule', `content` TEXT NOT NULL COMMENT 'Message content', `time_to_send` NOT NULL COMMENT 'When the message should arrive at the user', `campaign_fk` INT unsigned NULL DEFAULT NULL COMMENT 'the foreign key of the campaign this message belongs to', `user_id` INT unsigned NOT NULL COMMENT 'The user id this message is generated for', `pool_id` INT unsigned NOT NULL COMMENT 'The pool node id this message was generated from', `message_template_id` INT unsigned NOT NULL COMMENT 'The node id of the message template this message was generated from', PRIMARY KEY (`id`), INDEX `CAMPAIGN` (`campaign_fk`), INDEX `MESSAGE_TEMPLATE` (`message_template_id`), INDEX `POOL` (`pool_id`), INDEX `USER` (`user_id`) ) ENGINE = InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT 'The base table for myproject message instances'; Array ( ) in db_create_table() (line 2684 of /Users/xxx/Repos/myproject/includes/database/database.inc).
I searched for errors in the schema definition but could not find any. Also I don't see what exactly is wrong with the sql statement.
My server setup is:
Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2 PHP/5.3.6 with Suhosin-Patch
MySQL client version: mysqlnd 5.0.8-dev - 20102224 - $Revision: 308673 $
Does anyone see whats wrong here?
I think you need to use datetime:normal as the key for your datetime column type.
'time_to_send' => array(
'description' => 'When the message should arrive at the user',
'type' => 'datetime:normal',
'not null' => TRUE,
),
Other than that you can try specifying the column type as a MySQL DATETIME explicitly:
'time_to_send' => array(
'description' => 'When the message should arrive at the user',
'mysql_type' => 'DATETIME',
'not null' => TRUE,
),
The problem seems to stem from whether or not DATETIME is actually allowed in Drupal 7; see this discussion for a lot more details than I can fit in here :)

Categories