once a day I need to remove all data from 2 tables and upload new data. All goes well with "product" table, but I'm not able to populate "pricelist" table because there is foreign key relationship on the table.
model.py:
class product(models.Model):
EAN = models.CharField(primary_key=True, unique=True, max_length=13)
Name = models.CharField(max_length=50)
class vendor(models.Model):
vendor = models.CharField(primary_key=True, unique=True, max_length=7)
Name = models.CharField(max_length=50)
class pricelist(models.Model):
EAN = models.ForeignKey(product, on_delete=models.CASCADE)
vendor = models.ForeignKey(vendor, on_delete=models.CASCADE)
Qty = models.CharField(max_length=15)
app.py ...
def app
...
product_list = product_list[["EAN", "Name"]]
session.execute("use intranet; SET FOREIGN_KEY_CHECKS = 0; DELETE FROM product_list; DELETE FROM pricelist; SET FOREIGN_KEY_CHECKS = 1;")
session.flush()
session.commit()
product_list.to_sql("product_list", con=engine, if_exists='append', chunksize=1000, index=False)
df = df[["EAN", "vendor", "Qty"]]
# everything works until here:
df.to_sql("pricelist", con=engine, if_exists='append', chunksize=1000, index=False)
Error I get:
(1452, 'Cannot add or update a child row: a foreign key constraint fails
If I replace if_exists='append' with if_exists='replace' then data is uploaded but table settings (including all relations) are wiped out.
Any help would be really appreciated!