4

I am working on a project where, I have been assigned a task to create user management for the application. But I have stuck on the table relationship and their migration.

Effort

I have these tables:

  1. Users
    • user_id
    • username
    • password
  2. Profiles
    • profile_id
    • user_id
    • firstname
    • lastname
    • email
  3. Address

    • address_id
    • profile_id
    • address
    • city
    • state
    • country
    • pincode
  4. Configurations
    • config_id
    • configuration_name
    • configuration_type
    • parent_id

Now I have to create model and migration for the same above structure. For this i have create/modify below model and migration class.

Model: User

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'password',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function profile()
    {
        return $this->hasOne('Profile','user_id');
    }
}

Migration: 2014_10_12_000000_create_users_table.php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('user_id');
            $table->string('username');
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

Model: Profile

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    public function user(){
        return $this->belongsTo('User');
    }
    public function address()
    {
        return $this->hasOne('Address','address_id');
    }
}

Migration: 2016_02_26_101749_create_profiles_table.php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProfilesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('profiles', function (Blueprint $table) {
            $table->increments('profile_id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
            $table->string('lastname')->nullable();
            $table->string('firstname')->nullable();
            $table->string('gender')->nullable();
            $table->string('email')->unique();
            $table->string('phonenumber', 20)->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('profiles');
    }
}

Model: Addess

namespace App;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    public function profile(){
        return $this->belongsTo('Profile');
    }

    public function city() {
        return $this->hasOne('Configuration', 'config_id');
    }

    public function state() {
      return $this->hasOne('Configuration', 'config_id');
    }

    public function country() {
        return $this->hasOne('Configuration', 'config_id');
    }
}

Migration: 2016_02_26_102805_create_addresses_table.php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAddressesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('addresses', function (Blueprint $table) {
            $table->increments('address_id');
            $table->integer('profile_id')->unsigned();
            $table->foreign('profile_id')->references('profile_id')->on('profiles')->onDelete('cascade');
            $table->string('address')->nullable();
            $table->integer('city')->unsigned();
            $table->foreign('city')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->string('pincode')->nullable();
            $table->integer('state')->unsigned();
            $table->foreign('state')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->integer('country')->unsigned();
            $table->foreign('country')->references('config_id')->on('configurations')->onDelete('cascade');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('addresses');
    }
}

Model: Configuration

namespace App;

use Illuminate\Database\Eloquent\Model;

class Configuration extends Model
{
    public function children() {
        return $this->hasMany('Configuration','parent_id');
    }
    public function parent() {
        return $this->belongsTo('Configuration','parent_id');
    }
   public function address(){
        return $this->belongsTo('Address');
    }
}

Migration: 2016_02_26_104519_create_configurations_table.php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateConfigurationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('configurations', function (Blueprint $table) {
            $table->increments('config_id');
            $table->string('configuration_name');
            $table->string('configuration_type');
            $table->string('parent_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('configurations');
    }
}

Now, when I run php artisan migrate I am getting error that :

migration error .

Please suggest me how to do that. I have to use same table structure and cannot modify it. If any further update require or I forgot something please let me know.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Mandy
  • 1,103
  • 3
  • 17
  • 38
  • i have created user_id not id in migration table. So, i think that is not the issue. I think issue is with the address and configuration as, address contain city, state,country columns and these columns will be the foreign key from configurations table. – Mandy Feb 26 '16 at 11:58
  • Yeah sorry i realised after i posted the comment that your column name is `user_id` in `user` table - confused me for a moment. I deleted the comment. – DavidT Feb 26 '16 at 11:59
  • Run migrations separetely to see which one is giving you the error. – Alexey Mezenin Feb 26 '16 at 12:05
  • Ok, I will try. I have tried, but i did not find anything to run seprate migration for each table. Please suggest me how to run migration sepratly for each model. – Mandy Feb 26 '16 at 12:12

1 Answers1

2

That happen because migration will try to migrate address table before configuration so it will not found the foreign key config_id you're referenced, so you could change the name of migrations files then the migration commad could pass the configurations_table migrate file first then the addresses_table migrate file, so just change :

2016_02_26_104519_create_configurations_table.php

To :

2016_02_26_102005_create_configurations_table.php
_____________^

After that you should run optimize command to regenerating optimized class loader :

php artisan o

And rerun php artisan migrate command now the problem should be solved.

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
  • 1
    Yes, It solved my issue. Could you please tell me is my model and their relationship is ok. – Mandy Feb 26 '16 at 12:23
  • Not sure, first time i see same column (`config_id`) represent different informations (city, state, country) not sure if it will works. – Zakaria Acharki Feb 26 '16 at 12:26