Skip to content

Problem with Not unique table/alias #4

Closed
@borisovish

Description

@borisovish
I have some problem with this code:
Syntax error or access violation: 1066 Not unique table/alias: 'orders_version' (SQL: select * from `orders` inner join `orders_version` on `orders`.`id` = `orders_version`.`ref_id` and `orders_version`.`version` = 1 inner join `orders_version` on `orders`.`id` = `orders_version`.`ref_id` and `orders_version`.`version` = `orders`.`latest_version` where `orders`.`id` = 1 limit 1)
My Schema:
  Schema::create('orders', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->morphs('import');
            $table->integer('latest_version');

            $table->integer('user_id')->nullable()->comment("Id usera");
            $table->integer('orderid_pp')->nullable();

            $table->integer('shop_id')->nullable();
            $table->integer('shop_revision')->nullable();

            $table->timestamp('action_at')->comment("дата клика");

            $table->timestamp('created_at');

            $table->index('orderid_pp');
            $table->index('shop_id');
            $table->engine = 'InnoDB';
        });

        Schema::create('orders_version', function (Blueprint $table) {
            $table->bigIncrements('revid');
            $table->integer('ref_id')->unsigned();
            // $table->integer('ref_id')->primary();
            $table->integer('version')->unsigned();

            $table->string('status', 20)->nullable()->comment("cтатус");

            $table->integer('order_card')->nullable();
            $table->string('order_card_cur', 11)->nullable();

            $table->integer('earning')->nullable());
            $table->string('earning_cur', 11)->nullable();

            $table->integer('cashback_total')->nullable()->comment("");
            $table->integer('cashback_subtotal')->nullable()->comment("");
            $table->string('cashback_cur', 11)->nullable();

            $table->timestamp('updated_at');
            $table->timestamp('deleted_at');
            $table->timestamp('approved_at')->nullable();

            $table->engine = 'InnoDB';
            $table->index('status');
            $table->index('order_card_cur');
            $table->index('earning_cur');
            $table->index('cashback_cur');
        });

My model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use \ProAI\Versioning\Versionable;
    // use \ProAI\Versioning\SoftDeletes;

    public $timestamps = true;
    public $versioned  = [
        'status',
        'order_card',
        'order_card_cur',
        'earning',
        'earning_cur',
        'cashback_total',
        'cashback_subtotal',
        'cashback_cur',
        'updated_at',
        'deleted_at',
        'approved_at',
    ];

}
Select:
$order = Order::version(1)->find(1);
 dd($order->toArray());

Olso i have problem with 
 $order = Order::allVersions()->get();
       sd($order->toArray());
I have exeption:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'orders_version' (SQL: select * from `orders` inner join `orders_version` on `orders`.`id` = `orders_version`.`ref_id` inner join `orders_version` on `orders`.`id` = `orders_version`.`ref_id` and `orders_version`.`version` = `orders`.`latest_version`)

Can you help me fix this problem ?




Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions