-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.2] Update column created_at, updated_at #11518
Comments
attribute CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP need to apply for the field updated_at Schema::create('users', function (Blueprint $table)
{
$table->increments('id');
$table->string('name', 32);
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
}); |
This is by design. |
@GrahamCampbell I do not quite understand, this is a mistake or a well conceived? |
I'm sorry, I don't quite understand what it is you're saying is incorrect here? |
Please look carefully at the post #11518 (comment) there migration, which creates a column with dates
after migration to created_at field is added to the attribute CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP and it is wrong example: Controller public function show ($id)
{
$news = News::findOrFail($id);
$news->views++;
$news->save();
return view('news.show', compact('news'));
} Similarly, if the migration was to create a field with the timestamp and attribute CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP then the created_at column value will be created without attribute CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in laravel 5.1 of the $table->timestamps(); were created alike:
Perhaps the best idea is to return as |
Are you sure this change is only on 5.2? I can't see any differences between the current 5.1 dev and 5.2 dev? |
What database are you using please? |
I've investigated this for you, and this breaking change was made in 5.2, and it was definitely intentional. |
I deliberately rolled round of audit on its project to ensure that the error was confirmed. the only problem is to laravel 5.2 presumably there https://github.com/laravel/framework/blob/5.2/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php#L532 DataBase Mysql please return the creation of fields with attributes that were previously
|
Yep, we changed the mysql grammar. |
Ping @taylorotwell. |
results of creating a table of migration Schema::create('users', function (Blueprint $table)
{
$table->increments('id');
$table->string('name', 32);
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
}); Schema::create('users', function (Blueprint $table)
{
$table->increments('id');
$table->string('name', 32);
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamp('new_time');
$table->timestamps();
}); |
I'm unable to recreate this... :/ |
I'm on Laravel 5.2.3 and get the following declaration:
|
I use laravel 5.2.5 and MySql Server 10.1.9-MariaDB and always get such a result (((
|
Hi Taylor, I am using Laravel 5.2.5 too and i have a same problem. I was using 5.1.27 and that problem is not exist. I upgraded to 5.2.5 then run php artisan migrate:refresh and created_at field properties changed. Migration command: $table->timestamps(); SQL:
|
I can reproduce the issue (Laravel 5.2.5, MySQL 5.5.45, apparently not affected by strict mode setting). Interestingly, the SQL code generated by Laravel looks fine:
So it's MySQL who adds the default values and the See: MySQL 5.5 - Automatic Initialization and Updating for TIMESTAMP |
@vlakoff, I'm on 5.6.21. |
Any news on this? Using $table->timestamp('created_at')->default(0);
$table->timestamp('updated_at')->default(0); in the meantime. |
Does this work on mysql 5.7, since laravel 5.2 only officially supports mysql 5.7 now? |
@GrahamCampbell - Was not aware of this. Should the docs not be updated? |
@GrahamCampbell - Just upgraded my server, which now uses MariaDB 10.1.9. Issue persists. |
@vlakoff I had no MySQL error using |
They solved this problem. Just composer update. |
@jarodrejestracyjny i don't see any new commits, what do you mean by 'they solved this problem?' |
I think ->nullableTimestamps() instead of ->timestamps() is the better solution right now.And we can wait for next release,to got a good news. |
hi every body i fixed problem create_at please do it: vendor/laravel/framework/srs/illuminate/database/schema/blueprint.php line:792 public function timestamps()
} |
I don't quite understand the status of this bug. I just tried the two migrations from the default install (using both
All 3 columns came out wrong. @GrahamCampbell I think this bug should be reopened. |
@arashlaghaei if you think it solves the problem, you can open a pull request. Do not forget to explain in it why you have done this fix, what does it fix, etc. |
I just tried performing the same migrations on PostgreSQL and the timestamp columns are created without any option or trigger to auto-populate them. They are just plain timestamp columns with NULL default value:
So are timestamp columns supported by any driver? |
Prevents MySQL assigning default CURRENT_TIMESTAMP Related issue: laravel/framework#11518
Prevents MySQL assigning default CURRENT_TIMESTAMP Related issue: laravel/framework#11518
Over a year since this issue has been opened. Using the default function for adding created_at and updated_at fields in my tables, it leaves me with a 'on update CURRENT_TIMESTAMP' on the first encountered timestamp of the table. Which isn't even close to the right one. How, after all this time, is this still an issue? EDIT: The issue only occurs when using another Column of the Type 'TIMESTAMP' besides the 'updated_at' and 'created_at' ones in the table. |
I'm having a bit of a different issue here. I have project that started out in 5.1, and has since been upgraded to 5.3. Most of the tables were created in 5.1, so they have the old "not nullable" The problem is, when I try to run a migration to ALTER, the OLD pre-5.2 tables.
Even though the fields I'm changing have nothing to do with timestamps, I'm unable to alter the table. Additionally, I cannot migrate the timestamps to the new "nullable" ones because: Not really sure what to do about this. |
@Nikita240 What version of MySQL are you using? |
@a-komarev v5.6.27 |
Since |
In your migration file please define first the "update_at" column then define the "create_at" column. In Laravel first coming column with default time_stamp value automatically set the attribute "on update". Now your migration file looks like below: $table->timestamp('update_at'); |
Wow still had this issue on mysql5.7 and laravel 7.x... Just one timestamp in the schema
Makes it CURRENT_TIMESTAMP on update.. I was very surprised because this behavior overwrites data.. This should not happen by default, only if explicitly enabled Eg
|
You can try using this
|
Four and a half years later I ended up here because of the I have a migration that kind of looks like this:
Laravel will compile this into the correct SQL:
MySQL has this little thing in their documentation though:
That's why The real problem for me is that I am using the Besides, this thing from the MySQL documentation kind of does worry me:
|
I cannot for the life of me find out how to add the updated at column with "on update CURRENT_TIMESTAMP" Its just so much quicker to use PHPmyadmin and do it manu8ally instead of trying 10 different ways, creating 10 different migration files and deleting the darn table 10 times only to find I still cannot add an updated_at column with auto update. How is this not in the documentation? |
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP')); |
Set timestamps automatically update on create/update
I would not suggest auto population at database end. |
@LowSociety was right. It was an unwanted unintended default on the MySQL database side. It screwed me up big time and had wasted 2 days to debug the Issue. |
Hello.
When working with Elokuent that it updates column created_at, updated_at both when creating and updating.
When creating this is true, but why update created_at column every time I do something it updated?
In this case, it makes use of a useless $tablet->timestamps(); because you can not fix the field of creating records
Say it is a bug or by design?
or tell me how to leave the field after creating created_at intact?
The text was updated successfully, but these errors were encountered: