Updating a large number of foreign key references in Django - django

I have a companies and employees tables, along with a third employees records table which contains a foreign key to both the companies and employees tables.
The company table has an entry of 'Company A'. Subsequently, 'Company A' splits into two new entries: 'Company A1' and 'Company A2'.
Now in the employees records table, currently a large number employees are associated with 'Company A'.
For each employee associated with 'Company A', I want to create two new entries which associates the employee with both 'Company A1' and 'Company A2'.
What would be the optimal way to accomplish this with Django? Considering that there could be hundreds of thousands or possible millions of rows of employee records that need updating.

Related

How to multiply to columns in different table and get another column as output in django orm

I have three tables :
First one is product table consist of product details and price
Second one is customer table with customer details
And a cart table with customer_id and product _id as foreign key and qty field for quantity of product
I want join cart table and product table and get an additional column called total price that is the result of price in product *qty in cart
How to do this in django orm
I tried f function but it didn't work
You can try this.
Cart.objects.annotate(total_price=F('product__price')*F('qty'))

How to create model where a table represents both fact and dim?

I have following tables:
User: userid, username
Customer: customerid, customer name, userid
Sales: salesid, customerid, userid
User filters sales and customers. And also User and customer should be able to filter sales.
How to make a star schema of the above?
Is it correct to duplicate the customer table such that 1 is a dim and other is fact?
Should the both customer tables link to each other or not have Link?

Django: how to save model into different database tables depending on which ManyToOne relationship it belongs to?

I'm new to Django. My app have two models, Customer and Order, where each customer can have multiple orders. So what I did is to set up a ForeignKey(Cusotmer) relationship in Order model:
name = models.CharField(max_length=20)
phone = models.CharField(max_length=20)
def __str__(self):
return self.name
class Order(models.Model):
customer= models.ForeignKey(Customer,null=True, on_delete = models.SET_NULL)
ordername = models.CharField(max_length=20)
In this case, if I want to query the orders by a specific customer customer1, I can use Order.objects.filter(Customer=customer1). But I wonder if it is possible to save the order data for each customer in a separate database table? Right now all the orders are saved in the same table and misuse of filters can let customers view other customers' orders.
But I wonder if it is possible to save the order data for each customer in a separate database table?
Even if this were possible, you definitely do not want to store each customer's orders in a separate database table. Relational databases are designed for data with the same structure to be stored as rows in a table, and that's how Django models (which define the structure of your data) expect to interact with your database.
The correct approach is to save all customers' orders in a single table, using your Order model, and use the Django ORM to filter based on the user.
Right now all the orders are saved in the same table and misuse of filters can let customers view other customers' orders.
Even if you used separate tables for each customer's orders, you would still need to determine which table's data to display to the user (just like how with all the orders in a single table, you have to determine which rows to use). If you use the Django ORM correctly (to filter the data based on the user who makes the request), this is a non-issue.

On what basis is a decision to be made regarding denormalizing dimension tables?

Say I want to create a product dimension. It will have product id as primary key, product name as value.
The sales fact table will link to the product dimension on the product id key.
In the OLTP system the product table has normalized relationship with Product sub category table. And product sub category table has relationship to product category table.
I am trying to understand - On what basis or factors is a decision to be made regarding denormalizing tables and having the product category and sub category values into the product dimension table.

Efficient Django query to include the primary contact from a list of Employers

I have a list of employers, each of which have multiple contacts. One of those contacts may have been designated as primary for that organization.
class Employer(models.Model):
pass
class EmployerContact(models.Model):
employer = models.ForeignKey(Employer)
primary = models.BooleanField(default=False)
I need to display a list of employers and their primary contacts.
If I use Employer.objects.all(), I get all of them but then need an extra query to get the primary contact per employer, on a long list, this is not very efficient.
If I select on primary contacts and .select_related('employer') I get all the employers that have primary contacts, but miss those that don't have any contact (or maybe a primary contact has not yet been selected).
How can I get the list of Employers with their primary contact efficiently without missing employers with no primary contact?
Is there someway using .extra maybe?
You could use prefetch_related (which allows you to cache relationships that select_related can't) to first fetch and cache the related EmployeeContact objects, and then do the logic for finding if they have primary=True or not in python.
This would do it in two queries:
employees = Employee.objects.all().prefetch_related("employeecontact_set")
l = []
for e in employees:
for c in e.employeecontact_set.all():
if c.primary:
l.append(e.pk)
employees = employees.filter(pk__in=l)