Eduardo G. Posted August 10, 2022 Share Posted August 10, 2022 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 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 10, 2022 Share Posted August 10, 2022 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 0 Quote Link to comment Share on other sites More sharing options...
Eduardo G. Posted August 10, 2022 Author Share Posted August 10, 2022 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 0 Quote Link to comment Share on other sites More sharing options...
pRieStaKos Posted August 10, 2022 Share Posted August 10, 2022 (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 August 10, 2022 by pRieStaKos 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.