Jump to content
Sign in to follow this  
Eduardo G.

Default TimeStamp value on create table

Recommended Posts

Hi all I have a strange problem.

When creating a table from my addon's activate() method I get a default value for the first timestamp field.
For example:

       Capsule::schema()->create('sample_table', function($table) {
           $table->increments('table_id');
           $table->integer('age');
           $table->string('name', 100);
           $table->timestamp('created');
           $table->timestamp('updated');
           $table->float('amount', 8, 2);
           $table->text('description');
       });

This creates a table like this:

CREATE TABLE `sample_table` (
  `table_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `age` int(11) NOT NULL,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `created` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `amount` double(8,2) NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`table_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

See default value for "created" and "updated"?

Can someone explain why? Or how should I get it filled by zeros like the other timestamp field?

Thank you

Share this post


Link to post
Share on other sites
9 hours ago, Eduardo G. said:

Hi all I have a strange problem.

When creating a table from my addon's activate() method I get a default value for the first timestamp field.
For example:


       Capsule::schema()->create('sample_table', function($table) {
           $table->increments('table_id');
           $table->integer('age');
           $table->string('name', 100);
           $table->timestamp('created');
           $table->timestamp('updated');
           $table->float('amount', 8, 2);
           $table->text('description');
       });

This creates a table like this:


CREATE TABLE `sample_table` (
  `table_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `age` int(11) NOT NULL,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `created` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `amount` double(8,2) NOT NULL,
  `description` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`table_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

See default value for "created" and "updated"?

Can someone explain why? Or how should I get it filled by zeros like the other timestamp field?

Thank you

Try

$table->timestamp('created')->useCurrent();

https://laravel.com/docs/7.x/migrations#creating-tables

Share this post


Link to post
Share on other sites

As I read:

->useCurrent() Set TIMESTAMP columns to use CURRENT_TIMESTAMP as default value

Thats not what I'm trying, just want the first timestamp field's defaults works just like the second one

Share this post


Link to post
Share on other sites
Posted (edited)
6 minutes ago, Eduardo G. said:

As I read:

->useCurrent() Set TIMESTAMP columns to use CURRENT_TIMESTAMP as default value

Thats not what I'm trying, just want the first timestamp field's defaults works just like the second one

Then try 

$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

OR

$table->timestamps()->useCurrent()->useCurrentOnUpdate();
$table->timestamps(0); Adds nullable created_at and updated_at TIMESTAMP equivalent columns with precision (total digits).
Edited by pRieStaKos

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated