Learning Laravel Those pits (ii) Data migration

Source: Internet
Author: User
Tags character set table name

Directory:

Laravel 5.4:specified Key was too long error sqlstate[42s01]: Base table or view already exists:1050 pdoexception::("SQL STATE[42000]: Syntax error or Access violation:1067 Invalid default value for ' Published_at ') set nullable timestamp: Set default timestamp pdoexce Ption::("SQLSTATE[42S22]: Column not found:1054 Unknown column ' Created_at ' in ' Field List ') Repeat make:migration php Artisa n db:seed table name variable plural singular

Laravel 5.4:specified Key was too long error

Cause: From LV 5.4 The database default character set is UTF8MB4 (includes support for emojis). If you are using MySQL v5.7.7 or later, you do not need to make any modifications.

Children's shoes with earlier versions of MySQL database (including mariadb) can be modified so as to modify the file/project/app/providers/appserviceprovider.php

Use Illuminate\support\facades\schema; Note to introduce the namespace public

 function boot ()
    {
        Schema::d efaultstringlength (191);//For early MySQL data migration
    }

Then re-use the migration command:

PHP Artisan Migrate
sqlstate[42s01]: Base table or view already exists:1050

The table already exists when migrating data

Workaround:

Delete the tables that already exist, and then migrate again.

The migration results are as follows:
pdoexception::("sqlstate[42000]: Syntax error or Access violation:1067 Invalid default value for ' Published_at '")

Modification method One: Vim config/database.php

' MySQL ' = [
     ' driver ' = ' mysql ',
...
     ' Prefix ' + ' ', 
     ' strict ' = = true,//modify here
     ' engine ' = null,
 ],  

Modified to:

' MySQL ' = [
     ' driver ' = ' mysql ',
...
     ' Prefix ' + ', 
     ' strict ' and false,//modify here
     ' engine ' = null,
 ],  
set a time stamp that can be empty:
Set the default time stamp
$table->timestamps (' created_at ');//Generates CREATED_AT\UPDATED_AT fields
pdoexception::("SQLSTATE[42S22]: Column not found:1054 Unknown column ' Created_at ' in ' Field List ')

This issue occurs because the table migration does not have a default timestamp field set, but using the factory method to generate the seed data, you can modify

Public function up ()
    {   
      schema::create (' posts ', function (Blueprint $table) {
      $table->increments (' id ') ;
      $table->string (' slug ')->unique ();
      $table->string (' title ');
      $table->text (' content ');
      $table->timestamps (); Add this line
      }); 
   
Repeat Make:migration

In fact, this is not a pit, because the operation itself is just a

But for the novice, may inadvertently repeated execution several times, and make:migration will not error;
The problem comes when you perform the migration:

The solution is to delete or modify the schema name of the same table.
PHP artisan db:seed table name variable plural singular

This may be related to Chinglish, the foreigner used to write the table name as a plural, so simply the default model corresponding to the table name is the plural form of the English word
Therefore, to override the $table property in the model, such as:

protected $table = ' student ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.