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

Foreign key referencing self causes wrong dependency #163

Closed
tpaksu opened this issue Apr 26, 2020 · 3 comments
Closed

Foreign key referencing self causes wrong dependency #163

tpaksu opened this issue Apr 26, 2020 · 3 comments
Labels

Comments

@tpaksu
Copy link
Contributor

tpaksu commented Apr 26, 2020

Hello,

Suppose you have a migration like this, and you are using base classes:

Schema::create("categories", function(Blueprint $table){
    $table->bigIncrements('id');
    $table->string('title');
    $table->unsignedBigInteger('parent_id');
    $table->timestamps();
});
Schema::table("categories", function(Blueprint $table){
    $table->foreign('parent_id')->references('id')->on('categories');
});

Which produces:

<?php

/**
 * Created by Reliese Model.
 */

namespace App\Base;

use App\Category;
use App\Event;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Category
 *
 * @property int $id
 * @property string $title
 * @property int $parent_id
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property string $deleted_at
 *
 * @property Category $category
 * @property Collection|Category[] $categories
 * @property Collection|Event[] $events
 *
 * @package App\Base
 */
class Category extends Model
{
    use SoftDeletes;
    protected $table = 'bszcategories';
    public static $snakeAttributes = false;

    protected $casts = [
        'parent_id' => 'int'
    ];

    public function category()
    {
        return $this->belongsTo(Category::class, 'parent_id');
    }

    public function categories()
    {
        return $this->hasMany(Category::class, 'parent_id');
    }

    public function events()
    {
        return $this->hasMany(Event::class);
    }
}

and

<?php

namespace App;

use App\Base\Category as BaseCategory;

class Category extends BaseCategory
{
    protected $fillable = [
        'title',
        'parent_id'
    ];
}

Gives error on use App\Category; line, because it creates a circular reference, which is not needed and wrong. When you remove use App\Category; in the base class, everything works fine. Just a heads up.

@CristianLlanos
Copy link
Member

Hi, @tpaksu, thanks for pointing it out. PRs are welcome to fix this.

@CristianLlanos
Copy link
Member

It seems PR #158 solved it before. Release v0.0.15 has been made with the changes.

@tpaksu
Copy link
Contributor Author

tpaksu commented May 1, 2020

@CristianLlanos didn't check that before submitting this issue, sorry. Glad it has been solved ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants