I tried to followed the codeigniter tutorail on youtube here about creating migration in codeigniter. However, I got error
No migration could be found with the version number: 1
I already set $config['migration_version'] = 1; in Application/Config/migration.php and my migration file for creating users table
Application/migrations/001_Create_User.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Create_Users extends CI_Migration {
/*
up function is for creating and alert table
*/
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '128',
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->add_key('id',TRUE);
$this->dbforge->create_table('users');
}
/*
down function for rollback table
*/
public function down()
{
$this->dbforge->drop_table('users');
}
}
?>
When I check my database, I saw migration table version is always 0.
Please help me, thanks
In config/migration.php
/*
|--------------------------------------------------------------------------
| Migration Type
|--------------------------------------------------------------------------
|
| Migration file names may be based on a sequential identifier or on
| a timestamp. Options are:
|
| 'sequential' = Default migration naming (001_add_blog.php)
| 'timestamp' = Timestamp migration naming (20121031104401_add_blog.php)
| Use timestamp format YYYYMMDDHHIISS.
|
| If this configuration value is missing the Migration library defaults
| to 'sequential' for backward compatibility.
|
*/
$config['migration_type'] = 'sequential';
Related
I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
I use migration files (001_create_authors.php up to 005_create_comments.php) to automatically create the necessary database tables.
In addition to creating the categories table, I need to insert the default "Uncategorized" category in it, as posts must belong in a category.
The migration's current code:
class Migration_Create_Categories extends CI_Migration
{
public function up()
{
$this->dbforge->add_field(array(
'id'=>array(
'type'=>'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'author_id'=>array(
'type'=>'INT',
'constraint' => 11,
'unsigned' => TRUE,
),
'name'=>array(
'type'=>'VARCHAR',
'constraint' => 255,
),
'created_at'=>array(
'type'=>'TIMESTAMP',
)
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('categories');
}
public function down()
{
$this->dbforge->drop_table('categories');
}
}
What must I add to he above code in order to insert the default "Uncategorized" category in the categories table, at the first migrations run?
$this->dbforge->create_table('categories');
//after this line
$data = array(
//'id' => leave this as it will auto created
'author_id' => 1,
'name' => 'Uncategorized'
//'created_at' => leave it as this will auto created
);
$this->db->insert('categories', $data);
My migration is not working. I receive "Migration Work" in my browser but no blog table created which is what I expect.
This checks the revision number in migration.php and then looks for a file that
begins with that number (eg. 001_) in migrations folder.
<?php
// this is controller/Migration.php
class Migration extends CI_Controller {
function index() {
$this->load->library('migration');
if ( ! $this->migration->current()) {
show_error($this->migration->error_string());
} else {
echo "Migration Worked";
}
}
}
This is 002_install_blog.php It creates the blog table.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Migration_Install_blog extends CI_Migration {
public function up()
{
// Drop table 'blog' if it exists
$this->dbforge->drop_table('blog', TRUE);
// Table structure for table 'blog'
$this->dbforge->add_field(array(
'id' => array(
'type' => 'MEDIUMINT',
'constraint' => '8',
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'slug' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'body' => array(
'type' => 'TEXT',
),
));
$this->dbforge->add_key('id', TRUE);
$this->dbforge->create_table('blog');
}
public function down()
{
$this->dbforge->drop_table('blog', TRUE);
}
}
This is migration.php. It holds the migration version. It is set to 2 in order to use 002_install_blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$config['migration_enabled'] = TRUE;
$config['migration_type'] = 'sequential';
$config['migration_table'] = 'migrations';
$config['migration_auto_latest'] = TRUE;
$config['migration_version'] = '2';
$config['migration_path'] = APPPATH.'migrations/';
I figured this out. I have a table called 'migrations'. I incorrectly dropped my table inside of mysql and did not by running the migration file. Then, I ran the migration file, however, the version number in the 'migrations' table was still set to 2. I set the version number to 1 manually and then ran the migration file. Looked at the version number in the migrations table after running it and sure enough the version was 2.
Moral of the story. No need to drop tables manually. It should be all handled in the migration file!
I have read other answers but nothing solved my issue. When I run php artisan db:seed nothing happens, even no error is thrown.This is my DatabaseSeeder class
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
// call our class and run our seeds
$this->call(BearAppSeeder::class);
// $this->call(UserTableSeeder::class);
Model::reguard();
}
}
BearAppSeeder contains following content.
<?php
use Illuminate\Database\Seeder;
class BearAppSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// clear our database ------------------------------------------
/*DB::table('bears')->delete();
DB::table('fish')->delete();
DB::table('picnics')->delete();
DB::table('trees')->delete();
DB::table('bears_picnics')->delete();*/
// seed our bears table -----------------------
// we'll create three different bears
// bear 1 is named Lawly. She is extremely dangerous. Especially when hungry.
$bearLawly = Bear::insert(['name' => 'Lawly',
'type' => 'Grizzly',
'danger_level' => 8]);
/*/ bear 2 is named Cerms. He has a loud growl but is pretty much harmless.
$bearCerms = Bear::create(array(
'name' => 'Cerms',
'type' => 'Black',
'danger_level' => 4
));
// bear 3 is named Adobot. He is a polar bear. He drinks vodka.
$bearAdobot = Bear::create(array(
'name' => 'Adobot',
'type' => 'Polar',
'danger_level' => 3
));
$this->command->info('The bears are alive!');
// seed our fish table ------------------------
// our fish wont have names... because theyre going to be eaten
// we will use the variables we used to create the bears to get their id
Fish::create(array(
'weight' => 5,
'bear_id' => $bearLawly->id
));
Fish::create(array(
'weight' => 12,
'bear_id' => $bearCerms->id
));
Fish::create(array(
'weight' => 4,
'bear_id' => $bearAdobot->id
));
$this->command->info('They are eating fish!');
// seed our trees table ---------------------
Tree::create(array(
'type' => 'Redwood',
'age' => 500,
'bear_id' => $bearLawly->id
));
Tree::create(array(
'type' => 'Oak',
'age' => 400,
'bear_id' => $bearLawly->id
));
$this->command->info('Climb bears! Be free!');
// seed our picnics table ---------------------
// we will create one picnic and apply all bears to this one picnic
$picnicYellowstone = Picnic::create(array(
'name' => 'Yellowstone',
'taste_level' => 6
));
$picnicGrandCanyon = Picnic::create(array(
'name' => 'Grand Canyon',
'taste_level' => 5
));
// link our bears to picnics ---------------------
// for our purposes we'll just add all bears to both picnics for our many to many relationship
$bearLawly->picnics()->attach($picnicYellowstone->id);
$bearLawly->picnics()->attach($picnicGrandCanyon->id);
$bearCerms->picnics()->attach($picnicYellowstone->id);
$bearCerms->picnics()->attach($picnicGrandCanyon->id);
$bearAdobot->picnics()->attach($picnicYellowstone->id);
$bearAdobot->picnics()->attach($picnicGrandCanyon->id);
$this->command->info('They are terrorizing picnics!');*/
}
}
I have tried running composer dump-autoload but this did not resolve my issue. I am new to Laravel so I do not know too much about it.
I think your model class Bear is not found in BearAppSeeder. So ,
put
use App\Bear;
after
use Illuminate\Database\Seeder;
I hope this may help
Call the seeder class in following way:
$this->call(App\BearAppSeeder::class);
Use App directory name before BearAppSeeder::class
I am creating migrations in codeigniter. But I get an error saying that migrations are not found. I consulted official codeigniter docs. But in google, I couldn't find this error.
I created migrations folder and added following file.
001_Create_users.php
<? php
/**
* Description of 001_create_users
*
* #author Isuru
*/
class Migration_Create_users extends CI_Migration {
public
function up() {
$this - > dbforge - > add_field('id');
$this - > dbforge - > add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
//$this->dbforge->add_key('id', TRUE);
$this - > dbforge - > create_table('users');
}
public function down() {
$this - > dbforge - > drop_table('users');
}
}
This is the migration.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| Enable/Disable Migrations
|--------------------------------------------------------------------------
|
| Migrations are disabled by default for security reasons.
| You should enable migrations whenever you intend to do a schema migration
| and disable it back when you're done.
|
*/
$config['migration_enabled'] = TRUE;
/*
|--------------------------------------------------------------------------
| Migration Type
|--------------------------------------------------------------------------
|
| Migration file names may be based on a sequential identifier or on
| a timestamp. Options are:
|
| 'sequential' = Default migration naming (001_add_blog.php)
| 'timestamp' = Timestamp migration naming (20121031104401_add_blog.php)
| Use timestamp format YYYYMMDDHHIISS.
|
| If this configuration value is missing the Migration library defaults
| to 'sequential' for backward compatibility.
|
*/
$config['migration_type'] = 'timestamp';
/*
|--------------------------------------------------------------------------
| Migrations table
|--------------------------------------------------------------------------
|
| This is the name of the table that will store the current migrations state.
| When migrations runs it will store in a database table which migration
| level the system is at. It then compares the migration level in this
| table to the $config['migration_version'] if they are not the same it
| will migrate up. This must be set.
|
*/
$config['migration_table'] = 'migrations';
/*
|--------------------------------------------------------------------------
| Auto Migrate To Latest
|--------------------------------------------------------------------------
|
| If this is set to TRUE when you load the migrations class and have
| $config['migration_enabled'] set to TRUE the system will auto migrate
| to your latest migration (whatever $config['migration_version'] is
| set to). This way you do not have to call migrations anywhere else
| in your code to have the latest migration.
|
*/
$config['migration_auto_latest'] = TRUE;
/*
|--------------------------------------------------------------------------
| Migrations version
|--------------------------------------------------------------------------
|
| This is used to set migration version that the file system should be on.
| If you run $this->migration->current() this is the version that schema will
| be upgraded / downgraded to.
|
*/
$config['migration_version'] = 1;
/*
|--------------------------------------------------------------------------
| Migrations Path
|--------------------------------------------------------------------------
|
| Path to your migrations folder.
| Typically, it will be within your application path.
| Also, writing permission is required within the migrations path.
|
*/
$config['migration_path'] = APPPATH.'migrations/';
I tried to find a solution since yesterday, but I could not find a solution.
Change 'timestamp' to 'sequential' if you want to use '001'
You have a typo in 001_Create_users.php
<? php
and
- >
For version 3.1.10 the correct path for migrations is /application/migrations.
hth
plese be sure that name of migration folder under applications to be migrations.
Hope it helps!
I am following a tutorial and I have check the source code with my code to make sure there are no errors but I am unable to get codeigniter to create update or retrieve any records from my wamp sever(ver 2.4) when I enter public_html/admin/migration in the task bar codeigniter returns a message saying "Migration worked!" which is the result that is expected when codeigniter has updated the records but no changes are made to the database this is the code used in the controller
<?php
class Migration extends Admin_Controller
{
public function __construct ()
{
parent::__construct();
}
public function index ()
{
$this->load->library('migration');
if (! $this->migration->current()) {
show_error($this->migration->error_string());
}
else {
echo 'Migration worked!';
}
}
}
I have set the autoload libraries as follows $autoload['libraries'] = array('database');
is there something I am missing
here is my migration library file called '001_create_users.php'
<?php
class Migration_Create_users extends CI_Migration {
public function up()
{
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'email' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'password' => array(
'type' => 'VARCHAR',
'constraint' => '128',
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
));
$this->dbforge->create_table('users');
}
public function down()
{
$this->dbforge->drop_table('users');
}
}
Not sure if you are referring to the tutorial by Free Courses on youtube about cms buildout in codeigniter.
The way I fixed the issue (after a few hours) was debugging down to the environment setup.
Replace everything in your index function of the migration controller with var_dump($this->db) and see what it returns for your username/password/hostname/database, etc. If they are not what you expected per your database library setup, then your environment is not set properly in the index.php file.
I had to fix my case statement, had an extra / or \ can not remember which one, so the switch statement was forcing it to use the production environment configuration, which were not set.
add primary key before create table
$this->dbforge->add_key('id');