Welcome Guest, Not a member yet? Register   Sign In
DB Migration keeps failing
#1

Hi everyone,
I am trying to make a simple DB migration, renaming a column in an existing table. I tripled checked my migration file, connection and everything I could think of but I am stuck with an error and have no idea what to do. Can you help me understand where the problem is?
My migration:
PHP Code:
    public function up()
    {
        $fields = [
            "title" => [
                "name" => "title_en",
                "type" => "VARCHAR",
                "null" => false,
            ],
        ];
        $this->forge->modifyColumn("news"$fields);
    

The error I am getting:
Code:
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL' at line 2
Filename:    /Users/karine/Dev/ci-test/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 646
Any idea what could be going on?
Reply
#2

Most likely, there is data in your table that has the value title = NULL.
You cannot change such a column. Delete the found values with NULL or set them as "" (empty string)
Reply
#3

(08-26-2023, 12:31 PM)ozornick Wrote: Most likely, there is data in your table that has the value title = NULL.
You cannot change such a column. Delete the found values with NULL or set them as "" (empty string)

I have no Null value on that column
Reply
#4

It seems an SQL syntax error. Check the SQL statement that was executed.
Reply
#5

(This post was last modified: 08-26-2023, 03:20 PM by kcs.)

(08-26-2023, 02:14 PM)kenjis Wrote: It seems an SQL syntax error. Check the SQL statement that was executed.
How do I do that? Is it somewhere in the logs? Or should I use a different command when I run php spark migrate ?

// Edit
I am not sure if this is the best way... but I went into the BaseConnection.php file and added log_message('debug', $query); to get the query in the logs. It gives me this:

Code:
DEBUG - 2023-08-26 22:12:15 --> ALTER TABLE `news`
    CHANGE `title` `title_en` VARCHAR NOT NULL
Reply
#6

Oh, add number for varchar length 'constraint' => '100',
Reply
#7

That was it!!! I did not realise that I needed to resend all the constraints when modifying a column. Thanks a lot @ozornick.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB