Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrity constraint violation: How to join two tables

Tags:

join

php

mysql

I am making a program for a school project in laravel, so I try to join two tables: products and destinations

The table Product has the columns: id, name

The Destinations table has this columns: Destinations:id,product_id,destination,quantity,target_person

and I need to join product_id and id

products = DB::table('products')
    ->leftJoin('destinations','id','=','destinations.product_id ')
    ->get();

but when I try to use LEFT JOIN I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in on clause is ambiguous (SQL: select * from products inner join destinations on id = destinations.product_id)

like image 976
Csabikaaaa Avatar asked Sep 22 '17 08:09

Csabikaaaa


People also ask

What is the violation of referential integrity constraint?

It causes violation only if the tuple in relation 1 is deleted which is referenced by foreign key from other tuples of table 2 in the database, if such deletion takes place then the values in the tuple of the foreign key in table 2 will become empty, which will eventually violate Referential Integrity constraint.

What are the entity integrity constraints?

The entity integrity constraint states that primary key value can't be null. This is because the primary key value is used to identify individual rows in relation and if the primary key has a null value, then we can't identify those rows. A table can contain a null value other than the primary key field. 3. Referential Integrity Constraints

What is the difference between table-level integrity constraints and column level constraints?

The table-level Integrity constraints apply to the entire table, while the column level constraints are only applied to one column. Also Read: How to Create a Database In MySQL? The NOT NULL constraint prevents a column from having a NULL value. When no value is defined for a column, the DEFAULT Constraint provides a default value.

How to apply integrity constraints in MySQL?

You may apply integrity Constraints at the column or table level. The table-level Integrity constraints apply to the entire table, while the column level constraints are only applied to one column. Also Read: How to Create a Database In MySQL? The NOT NULL constraint prevents a column from having a NULL value.


2 Answers

use Table reference products.id

products = DB::table('products')
    ->leftJoin('destinations','products.id','=','destinations.product_id')
    ->get();
like image 69
Bilal Ahmed Avatar answered Oct 28 '22 07:10

Bilal Ahmed


Is because it does not know if 'id' is refering to the one in products or destinations. Try this:

products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')

->get();
like image 24
caryarit ferrer Avatar answered Oct 28 '22 05:10

caryarit ferrer