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 joindestinations
onid
=destinations
.product_id
)
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.
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
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.
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.
use Table reference products.id
products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With