• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issues with migration

#1
When I try to add a column in the existing table getting an error.

PHP Code:
<?php namespace App\Database\Migrations;

use 
CodeIgniter\Database\Migration;

class 
AddColumnCreatedAt extends Migration
{
    public function 
up()
    {
        $this->forge->addColumn('users''created_at');
    }

    
//--------------------------------------------------------------------

    
public function down()
    {
        
$this->forge->dropColumn('users''created_at');
    }
}

This is what i am doing

Getting error

Running all 
new migrations... 
PHP Code:
You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2 
PHP Code:
/var/www/html/appstart/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php 330 
Reply

#2
Best,

You forget to fill in what datatype 'create_at' is.
Example from the documentation.


PHP Code:
$fields = [
        'created_at' => ['type' => 'DATETIME']
];
$forge->addColumn('users'$fields);
// Executes: ALTER TABLE users ADD preferences DATETIME 

But can also use the model for filling the 'created at' field.

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
UserModel extends Model
{
        protected $table      'users';
        protected $primaryKey 'id';

        protected $returnType 'array';
        protected $useSoftDeletes true;

        protected $allowedFields = ['name''email'];

        protected $useTimestamps true;
        protected $createdField  'created_at';
        protected $updatedField  'updated_at';
        protected $deletedField  'deleted_at';

        protected $validationRules    = [];
        protected $validationMessages = [];
        protected $skipValidation     false;


Be sure you have added these field to your database.

André
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.