[英]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? 我有两个表,我试图
join
它们以获取所有引用的表,但出现错误,但是我不确定我做错了哪一部分,有人可以帮我吗?
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
我有此表,它保存票证
ticket_notes
注释,并且我使用ticketNumber
而不是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
我是用
query builder
来执行查询,而不是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))
我收到此错误
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'ticketNumber' in where clause is ambiguous (SQL: select
票.* from
门票inner join
ticket_notes on
门票.
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. 由于存在连接,因此有2列名为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.*');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.