Skip to content
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

Bug: Unknown column updated_at in 'field list' #8192

Closed
datamweb opened this issue Nov 11, 2023 · 5 comments
Closed

Bug: Unknown column updated_at in 'field list' #8192

datamweb opened this issue Nov 11, 2023 · 5 comments

Comments

@datamweb
Copy link
Contributor

datamweb commented Nov 11, 2023

PHP Version

8.2

CodeIgniter4 Version

CodeIgniter v4.4.3

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Windows

Which server did you use?

apache

Database

libmysql - mysqlnd 8.0.10

What happened?

Unknown column updated_at in 'field list'

Column updated_at is required for all tables!

Steps to Reproduce

composer create-project codeigniter4/appstarter CI4-bug
php spark make:migration Test
<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class Test extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'         => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'email'      => ['type' => 'varchar', 'constraint' => 255],
            'ip_address' => ['type' => 'varchar', 'constraint' => 255],
            'created_at'   => ['type' => 'datetime', 'null' => true],
        ]);
        $this->forge->addPrimaryKey('id');


        $this->forge->createTable('test', false);
    }

    public function down()
    {
        //
    }
}
php spark migratin --all
php spark make:model Test
<?php

namespace App\Models;

use CodeIgniter\Model;

class Test extends Model
{
    protected $table            = 'test';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'email'
    ];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];
}
<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
+model('Test')->insert([
+'email' =>'[email protected]',
+'ip_address' =>'127.127.0.0',
+]);
-return view('welcome_message');
    }
}

Expected Output

The presence of updated_at in the tables should not be mandatory.

Screenshot 2023-11-12 005830

Anything else?

If I add column updated_at to the table. The error is fixed.

I could create a separate field for the date here instead of using $useTimestamps, but the question is why didn't CI cover this.

@datamweb datamweb added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 11, 2023
@datamweb datamweb changed the title Bug: Unknown column created_at / updated_at in 'field list Bug: Unknown column updated_at in 'field list Nov 11, 2023
@kenjis kenjis removed the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 11, 2023
@kenjis
Copy link
Member

kenjis commented Nov 11, 2023

At least this is not a bug.

This requires that the table have columns named created_at, updated_at and deleted_at in the appropriate data type.
https://codeigniter4.github.io/CodeIgniter4/models/model.html?highlight=usetimestamps#dates

@kenjis kenjis added the enhancement PRs that improve existing functionalities label Nov 11, 2023
@kenjis
Copy link
Member

kenjis commented Nov 11, 2023

Try:

protected $updatedField = '';

@datamweb
Copy link
Contributor Author

@kenjis You always make me happy, it worked!
Thanks!

@kenjis
Copy link
Member

kenjis commented Nov 11, 2023

This is just a lack of documentation because that is how the code works.

@kenjis kenjis changed the title Bug: Unknown column updated_at in 'field list Bug: Unknown column updated_at in 'field list' Nov 11, 2023
@kenjis
Copy link
Member

kenjis commented Nov 12, 2023

It was documented.

Leave it empty to avoid update it (even $useTimestamps is enabled).
https://codeigniter4.github.io/CodeIgniter4/models/model.html?highlight=usetimestamps#updatedfield

@kenjis kenjis removed the enhancement PRs that improve existing functionalities label Nov 12, 2023
@kenjis kenjis closed this as completed Nov 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants