Home > hooks > How to implement a mysql date or datetime field correctly in hook_schema()?

How to implement a mysql date or datetime field correctly in hook_schema()?

April 11Hits:0
Advertisement

I have written a mymodule.install file which contains a schema definition to create a table in my database. This table contains two data fields. Theese fields will be populated by the users when they will fill in the specific content (example: to specific a publish date for a news). Now i didn't used the Date contributed module on purpose, cause i needed that theese two fields were on the same row in the database table for other reasons.

The hook_schema defines the two fields this way:

'pubblish_date' => array(     'description' => t('The pubblish date for the single news'),     'mysql_type' => 'datetime',     'not null' => FALSE,   ),   'unpublish_date' => array(     'description' => t('The unpublish date for the single news'),     'mysql_type' => 'datetime',     'not null' => FALSE,   ), 

The table is created correctly in the database, but i always get theese advice messages:

Field news_board.pubblish_date: no Schema type for mysql type datetime. Field news_board.unpublish_date: no Schema type for mysql type datetime. news_board.pubblish_date: no type for Schema type :normal. Field news_board.pubblish_date: no Schema type for type . news_board.unpublish_date: no type for Schema type :normal. Field news_board.unpublish_date: no Schema type for type .

Seems strange to me cause I red on the documentation to use the mysql_type specification in order to store the datetime format in mysql database.

I know that Drupal supports natively timestamps and if you want to store a different date format you have to use specific definitions like mysql_type or pgsql_type according on the database yuo are using.

In discussions I found online lot of people used mysql_type definition and from what I seen they solved the problem, so why it doesn't work for me?

Thanks a lot.

Answers

You should provide a fallback type (varchar) for the case that the database is not mysql.

'pubblish_date' => array(
  'description' => t('The pubblish date for the single news'),
  'type' => 'varchar',
  'mysql_type' => 'datetime',
  'not null' => FALSE,
),
'unpublish_date' => array(
  'description' => t('The unpublish date for the single news'),
  'type' => 'varchar',
  'mysql_type' => 'datetime',
  'not null' => FALSE,
),

However, the messages you see come from the Schema module. This problem is already recorded as issue #468644.

The implementation of the mysql datetime in your schema is fine. You can safely ignore these messages. I would say that these "advice messages" are caused by a bug in the Schema module.

Related Articles

  • How to implement a mysql date or datetime field correctly in hook_schema()?April 11

    I have written a mymodule.install file which contains a schema definition to create a table in my database. This table contains two data fields. Theese fields will be populated by the users when they will fill in the specific content (example: to spe

  • MySQL DATE vs DATETIME read/write performaceAugust 23

    Did anyone perform accurate read/write performance comparisons of MySQL's DATE vs DATETIME column types? Please do not bother writing "It should be faster" or "I think it's faster". I am looking for accurate performance measures and co

  • Change mysql date / time / datetime default output formatJanuary 28

    Is it possible for mysql to change the default date, time, datetime, timestamp etc. format? At this moment return 2015-12-31 I want to always show 2015.12.31 Without additional formatting. Is this possible? p.s. Maybe laravel can set the default date

  • MySQLi query a specific date in DATETIME fieldJanuary 30

    I am trying to query all records for a specific date. Right now, I am using: $date = '2016-01-30'; $query = $db->select("SELECT ID, OrderDate FROM store_orders WHERE OrderDate >= '$date 00:00:00' && OrderDate <= '$date 23:59:59'"

  • view 3: filter for datetime field is not working

    view 3: filter for datetime field is not workingDecember 2

    I have created a customised module that export the content of a remote database tables to the view interface, every thing works fine except of the filter of the datetime field. In my file: mytable.views.inc function mytable_views_data() { $data = arr

  • MYSQL-PHP handling datetime,timestamp timezone; how to correctly display and save post and update date and time of a postFebruary 1

    I need Advice; I am seriously confused. On my website people will be posting and updating their comments;so I created two columns, one for save the datetime of post and one the datetime of update. On insertion and Update I use the function NOW() in t

  • mysql indexing date vs datetime on large audit tableJanuary 29

    I have an audit table that is currently running at around 25M rows on innodb and growing about 1M rows a month. I currently have an index on the datetime column. For performance reasons, if a user searches it limits to the current day. Would index pe

  • How is INFORMATION_SCHEMA implemented in MySQL?

    How is INFORMATION_SCHEMA implemented in MySQL?June 15

    The INFORMATION_SCHEMA is, in theory, a set of views specified in the SQL standard that allow the user to inspect the system's metadata. How is it this implemented in MySQL? When I connect to a fresh installation I see two databases: mysql and inform

  • Drupal schema API not supporting MySQL date data type?May 17

    I am getting this error with my existing database: no Schema type for mysql type date. I did some research and it seems like schema does not support date type in MySQL. What would you guys suggest I replace it with? I'm presenting income statements a

  • MySQL 5.6 DateTime Incorrect datetime value: '2013-08-25T17:00:00+00:00' with Error Code 1292August 25

    I'm using MySQL 5.6 and I have a program that runs the following SQL statement: UPDATE m_table SET s_time = '2013-08-25T17:00:00+00:00' WHERE id = '123' against my database. Unforutnately, I get the following error: Incorrect datetime value: '2013-08

  • MySQL - Truncated incorrect datetime value on update statementSeptember 1

    I have an application which produces log files. These log files include a Timestamp field in the format 2015-08-25T09:35:01 UTC. (there will be apx 60 logs produced at 0.25 GB per day). I need to import these log files into MySQL for analysis. But I

  • Mysql datetime field partial matchSeptember 3

    Currently I am designing a mysql table structure that will hold a couple of million rows. I want to search in it on datetime field by date year and so on but the date will be entered by user so it may not be full date all the time. The field is index

  • ERROR when restoring MySQL data dumpAugust 24

    I am getting the following error when attempting to restore a MySQL data dump to a different server and a different MySQL version ... ERROR 1064 (42000) at line 14165: You have an error in your SQL syntax; check the manual that corresponds to your My

  • XAMPPErrorDomain error 1 for changing MySQL data dirsDecember 8

    I tried to move my mysql data dir from one disk to another, I modified the datadir parameter in my.cnf, but when XAMPP, and more precisely, MySQL starts, this error is displayed and MySQL doesn't run. Operation could not be completed. (XAMPPErrorDoma

  • MySQL data disappearing after restoring from backupJanuary 16

    I recently had to move from one hosting provider to another (if you're in the UK don't use Easyspace for your VPS!) as my previous host dumped my server without warning. Fortunately I had remote backups in place and I've been able to restore almost a

  • What's difference between this: sudo find /usr/local/mysql/data -type f -exec rm {} ';'June 4

    What's difference between this: sudo find /usr/local/mysql/data -type f -exec rm {} ';' and this: sudo rm -rf /usr/local/mysql/data --------------Solutions------------- sudo find /usr/local/mysql/data -type f -exec rm {} ';' sudo will make it run as

  • mysql Data loss after server restart June 23

    Possible Duplicate: mysql data loss after server crash After the server restarted due to a power cut, we lost all data saved in the database during the morning (client records created in the website). A few minutes before the crash, I was able to see

  • MySQL Data Modeling on netbook

    MySQL Data Modeling on netbookOctober 17

    Possible Duplicate: ER diagram software Looking for a MySQL data modeling toll, that will actually fit on my netbook screen. The official Workbench needs a higher resolution, so the bottom bit is cut off. Really annoying. Is there an alternative out

  • How to recover data from MySql Data files?October 23

    One of my servers' OS crashed yesterday. It was Win 2008.The server developed an HDD fault and did not boot. My host provider (Turnkey) has mounted the HDD on a Rescue system and given me access to the HDD through SSH. I have been able to download th

  • Periodic Mysql Data lossNovember 6

    Almost once in 2 weeks,i am facing mysql data loss in a table. almost 600 rows are being deleted. I cant trace that issue. What may cause this or how can i trace who/what makes it ? Thanks --------------Solutions------------- Check out aspersa's 'sta

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.888 s.