gpt4 book ai didi

Django,Sqlalchemy : Cannot drop table ganalytics_article because other objects depend on it

转载 作者:行者123 更新时间:2023-12-02 17:59:59 25 4
gpt4 key购买 nike

我的 Django 中有这个模型 ganalytics应用程序:

class Article(models.Model):
id = models.IntegerField(unique=True, primary_key=True)
article_title = models.CharField(max_length=250)
article_url = models.URLField(max_length=250)
article_pub_date = models.DateField()


class Company(models.Model):
company_name = models.CharField(max_length=250)


class Author(models.Model):
author_sf_id = models.CharField(max_length=20, null=True)
author_name = models.CharField(max_length=250)


class AuthorArticleCompany(models.Model):
author = models.ForeignKey(Author,
to_field="id",
on_delete=models.CASCADE,
related_name='authorarticle_author_id')
company = models.ForeignKey(Company,
to_field="id",
on_delete=models.CASCADE,
related_name='authorarticle_company_id')
article = models.ForeignKey(Article,
to_field="id",
on_delete=models.CASCADE,
related_name='authorarticle_article_id')




class Ganalytics(models.Model):
article = models.ForeignKey(Article,
on_delete=models.CASCADE,
related_name='ganalytics_author_id')
totalview = models.IntegerField()
totalinteractions = models.IntegerField()




class Unsubscribers(models.Model):
email = models.EmailField()
reasonwhy = models.CharField(max_length=90)


我正在运行 pandas to_sql 来上传数据库:
authorarticlecompanydf.to_sql("ganalytics_authorarticlecompany", con=engine, if_exists="append", index=False)
articledf.to_sql("ganalytics_article",con=engine,if_exists="replace",index=False)
company_name.to_sql("ganalytics_company",con=engine,if_exists="replace",index=False)
authordf.to_sql("ganalytics_author", con=engine,if_exists="replace", index=False)
我收到此错误消息:
DETAIL:  constraint ganalytics_ganalytic_article_id_d37f2464_fk_ganalytic on table ganalytics_ganalytics depends on table ganalytics_article
constraint ganalytics_authorart_article_id_7f4ff374_fk_ganalytic on table ganalytics_authorarticlecompany depends on table ganalytics_article
HINT: Use DROP ... CASCADE to drop the dependent objects too.

[SQL:
DROP TABLE ganalytics_article]
我试图改变 on_delete字段为不同的值,但它无济于事。
我究竟做错了什么?

最佳答案

您正在运行什么命令来获取错误?底线是Ganalytics取决于 Article通过他们之间的 FK。通过下降 Article首先你要打破这种依赖。您要么需要 DROP Ganalytics先然后Article或按照提示DROP ... CASCADE .虽然要知道会 DROP Ganalytics还。
更新
另一种选择是删除两个表之间的 FK。那么你就不会有依赖问题。

关于Django,Sqlalchemy : Cannot drop table ganalytics_article because other objects depend on it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63809018/

25 4 0