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

Migrations not working #3317

Closed
mridah opened this issue Jul 15, 2020 · 5 comments
Closed

Migrations not working #3317

mridah opened this issue Jul 15, 2020 · 5 comments

Comments

@mridah
Copy link

mridah commented Jul 15, 2020

I'm created 2 migrations using the commands below:

php spark migrate:create add_company
php spark migrate:create add_user

When I run php spark migrate, it shows both the migrations have run. Even php spark migrate:status shows both have been performed.

But in the database, only one table (company) is created (apart from the migration table). The user table is not created. I've tried this a couple of times but it's not working. I even tried using table name users instead of user but the same issue.

This is xxxx-xx-xx-xxxxxx_add_user.php:

class AddUser extends Migration
{
	public function up()
	{
		$this->forge->addField([
			'user_id'          => [
				'type'           => 'INT',
				'constraint'     => 5,
				'unsigned'       => TRUE,
				'auto_increment' => TRUE
			],
			'user_email'       => [
				'type'           => 'VARCHAR',
				'constraint'     => '80',
			],
			'user_password'       => [
				'type'           => 'VARCHAR',
				'constraint'     => '64',
			],
			'company_id'       => [
				'type'           => 'INT',
				'constraint'     => '3',
			],
			'added_on'       => [
				'type'           => 'TIMESTAMP',
				'default'     => 'CURRENT_TIMESTAMP',
			],
			'last_modified'       => [
				'type'           => 'TIMESTAMP'
			],
			'is_superadmin' => [
				'type'           => 'TINYINT',
				'default'           => 0,
			],
			'role' => [
				'type'           => 'ENUM("admin", "user")',
				'default'           => 'user',
				'null'			=> false,
			],
			'active' => [
				'type'           => 'TINYINT',
				'default'           => 0,
			],
		]);
		$this->forge->addKey('user_id', TRUE);
		$this->forge->createTable('user');
	}

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

	public function down()
	{
		$this->forge->dropTable('user');
	}
}

Why would this be happening ? @hex-ci @zahhar @lonnieezell @AndiKod

@mridah
Copy link
Author

mridah commented Jul 15, 2020

For those who are facing the same issue, it seems that 'type' => 'TIMESTAMP' is causing the issues.

So instead of

'last_modified' => [
    	'type'  => 'TIMESTAMP'
],

use

'last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP',

@paulbalandan
Copy link
Member

Maybe you need also to supply the 'default' key just like what you did in 'added_on'.

@michalsn
Copy link
Member

Well... that's how it should be handled. And it's documented here: https://codeigniter.com/user_guide/dbmgmt/forge.html?highlight=forge#passing-strings-as-fields

@4RSIM3R
Copy link

4RSIM3R commented Oct 1, 2022

So, the only way to fix this error is use plain string to create column schema ?

@michalsn
Copy link
Member

michalsn commented Oct 1, 2022

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

4 participants