简体   繁体   中英

SQLSTATE[23000]: Integrity constraint violation: 1052 using join with laravel

I have two tables and I am trying to join them in order to get all the referenced tables but getting errors but I am not sure which part I am doing wrong, can someone please give me a hand?

I have this table migration for tickets

public function up()
{
    Schema::create('tickets', function (Blueprint $table) {
        $table->increments('id');
        $table->string('ticketNumber')->nullable(true)->unique();
        $table->boolean('is_deleted')->default(false);
        $table->timestamps();
    });
}

I have this table which saves notes for the tickets ticket_notes and I am referencing using the ticketNumber instead of the id

public function up()
{
    Schema::defaultStringLength(191);
    Schema::create('ticket_notes', function (Blueprint $table) {
        $table->string('ticketNumber');
        $table->foreign('ticketNumber')->references('ticketNumber')->on('tickets');
        $table->increments('id');
        $table->string('rloc', 50);
        $table->boolean('is_deleted')->default(false);
        $table->timestamps();
    });
}

I am usin query builder to do the query instead of eloquent relationships

DB::table('tickets')->where([
    'ticketNumber' => 12345,
    'is_deleted'   => false,
])
    ->join('ticket_notes', 'tickets.ticketNumber', '=', 'ticket_notes.ticketNumber')
    ->select('tickets.*');

I am getting this error SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'ticketNumber' in where clause is ambiguous (SQL: select tickets .* from tickets inner join ticket_notes on tickets . ticketNumber = ticket_notes . ticketNumber where ( ticketNumber = 12345 and is_deleted = 0))

Anyone can give me a fresh look where I did wrong with the join? Thanks in advance for any suggestions / help.

Because of the join, there are 2 columns named ticketNumber. Specify which one you want. 'tickets.ticketNumber' => 12345

DB::table('tickets')->where([
    'tickets.ticketNumber' => 12345,
    'is_deleted'   => false,
])
->join('ticket_notes', 'tickets.ticketNumber', '=', 
    'ticket_notes.ticketNumber')
->select('tickets.*');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM