One-to-one mapping in sql - sql

I need to make an one-to-one relationship between two tables, it means, each passenger can only reserve one seat on a flight.
I have 4 tables:
Passenger : passengerId(PK), passengerName, PassengerAddress
Seat : seatId(PK), seatClass, flightId(Fk)
Flight : flightId(PK), flightDate
Reseveration : flight(PK), seatId(PK), passengerID(FK), reserveDate

Passenger : passengerId(PK),passengerName,PassengerAddress
//in this table make passneger id primary key as passenger will be unique with his details
Seat : seatId(PK),seatClass,flightId(Fk)
//in this table add a field say passenger id so that a passenger who is allotted in a flight will be mained here eg pasneger x in flight y.,also you can maintain a time for fligh such that if a passneger wantes to travel two times a day in a same flight
Fligth : flight Id(PK),flight Date
//make flight id unique hare
Reseveration: flight(PK),seatId(PK),passenger ID(FK),reserveDate
//this will be the final table reservation where you have all the details with no duplicacy.

Related

Update list of dates in SQL

I have a controller to make a room which needs a JsonBody in order to add the room:
{
"roomName": "Sol",
"properties": "Geluidsdichte kamer",
"capacity": 40,
"buildingName": "16A",
"location": "Leuven",
"reservableDates": ["2022-12-03", "2022-12-04", "2022-12-05"],
"imageUrl":"www"
}
Here we find a reservableDates object which is just a list of dates when the room is available for reservation
Now the backend code to put this code into the database isn't relevant for my problem so I will not state this here.
However the output I get in my database is this...
select * from rooms inner join rooms_reservable_dates
on room_id = rooms_room_id;
Now I have another function in my backend so I can update a room (For example change its available reservable dates, but the problem is that I don't know how to write the query so I can change the reservable dates while also updating the roomName for example.
I'm using JpaRepository in SpringBoot so I have to make a custom query for this.
In Postgresql I have 2 tables Rooms (with all the properties found in the picture except for the reservable_dates) and the other table is rooms_reservable_dates (which has the roomId and the dates that the room is available.
Thank you very much

Wikidata Query - Health Specialty of a Disease name

I want to query the "instance of P31" and "health specialty P115" for a disease name
"left bundle branch hemiblock (Q2143688)"
I want an output like :
left bundle branch hemiblock (Q2143688)
instance of : thoracic disease
health specialty :cardiology
Also is it possible to query all disease information with its instance of and health specialty, so that I can store it and process each time I need an information.

Django get objects that are foreign key of two models

I have the following three models where Budget and Sale both contain a foreign key to Customer:
class Customer(models.Model):
name = models.CharField(max_length=45)
# ...
class Budget(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.PROTECT)
# ...
class Sale(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.PROTECT)
# ...
I want to get a queryset of all Customer objects for which both a Budget and Sale exists. I initially tried getting the intersection of the customer field of all Budget and Sale objects:
customers = {
budget.customer for budget in Budget.objects.all()
} & {
sale.customer for sale in Sale.objects.all()
}
This returns the correct objects, but becomes horribly inefficient as the size of my database grows.
How can I retrieve these objects in a more efficient way? Thanks for any help!
You can filter with:
Customer.objects.filter(
budget__isnull=False,
sale__isnull=False
).distinct()
Django can follow ForeignKeys in reverse. It uses the related_query_name=… parameter [Django-doc] for the name of relation. If that is not specified, it falls back on the related_name=… parameter [Django-doc] parameter, and if that is not specified, it will use the name of the model in lowercase, so budget and sale. We here make LEFT OUTER JOINs on the Budget and Sale table, and check if for both there is a non-null row. Likely the Django ORM will optimize this to INNER JOINs.

VRP standstill query

What is purpose of Standstill in VRP? I am trying to understand below rule in VRP example. What is previousStandstill?
rule "distanceToPreviousStandstill"
when
$customer : Customer(previousStandstill != null, $distanceFromPreviousStandstill : distanceFromPreviousStandstill)
then
scoreHolder.addSoftConstraintMatch(kcontext, - $distanceFromPreviousStandstill);
end
previousStandstill is Vehicle or another Customer.
Ex.
ROUTE = VEHICLE(depot) -> CUSTOMER A -> CUSTOMER B -> CUSTOMER C -> CUSTOMER D
previousStandstill for CUSTOMER B is CUSTOMER A
previousStandstill for CUSTOMER A is VEHICLE
Vehicle location is the same as the location of depot.
So this rule add soft score for all distance in route except last part from CUSTOMER D to Vehicle

Joining table not directly related to main table in cakephp

I have three tables whose structure is is similar to below :
employees offices postings
________ ________ ___________
id id employees_id
name name offices_id
So I want to know how can I get the office name from Employee model. Putting office within $hasOne array shows Unknown column 'office.employees_id' in 'on clause'. What should I do to get the office name in the results ?
If you want to stick to your database, you might use has and belongs to many relation.
In employee.php:
public $hasAndBelongsToMany = [
'Office'=> [
'foreign_key' => 'employees_id',
'joinTable' => 'postings',
'associationForeignKey' => 'offices_id',
]
];
Then you can get office from employee model.
What I really want to suggest is that put a column office_id in employees table, and put
$public $belongsTo = ['office'];
in your employee model.
PS: Brackets [] is supported in php 5.4 or newer , if you are using php 5.3 or lower, you may want to replace it with array().