简体   繁体   English

在 sqlalchemy 中设置一对多关系,但不断收到 AmbiguousForeignKeysError

[英]Setting up One-to-Many relationship in sqlalchemy, but keep getting AmbiguousForeignKeysError

I'm trying to set up a one-to-many relation between two tables (User and BlogPost, respectively).我正在尝试在两个表(分别为 User 和 BlogPost)之间建立一对多的关系。 I tried copying what the course(?) solution for this and still get foreign-key errors.我尝试为此复制课程(?)解决方案,但仍然出现外键错误。 What do I have wrong?我有什么问题? Or, is this actually an issue with sqlalchemy (v. 1.3.19)?或者,这实际上是 sqlalchemy(v. 1.3.19)的问题吗?

I've even tried upgrading to the current version (1.4.31) with no success.我什至尝试升级到当前版本(1.4.31)但没有成功。 I was getting run-time errors (flask page error reports) usually around the posts = BlogPost.query.all() line in home() .我通常在home()中的posts = BlogPost.query.all()行周围遇到运行时错误(flask 页面错误报告)。 Now that I've upgraded the package, I'm getting a build-time error ( AttributeError: can't set attribute ) stemming from the db.create_all() line.现在我已经升级了 package,我收到了来自db.create_all()行的构建时错误( AttributeError: can't set attribute )。

I've also tried deleting the db, again with no success.我也尝试过删除数据库,但没有成功。

Please leave a comment if this was answered somewhere else or if more info is needed.如果在其他地方回答或需要更多信息,请发表评论。

Here's a snippet from Main.py:这是Main.py 的一个片段:

db = SQLAlchemy(app)

class User(db.Model, UserMixin):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(250), nullable=False)
    password = db.Column(db.String(250), nullable=False)
    name = db.Column(db.String(100), nullable=False)

    posts = relationship("BlogPost", back_populates="author")


class BlogPost(db.Model):
    __tablename__ = "blog_posts"
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    title = db.Column(db.String(250), unique=True, nullable=False)
    subtitle = db.Column(db.String(250), nullable=False)
    date = db.Column(db.String(250), nullable=False)
    body = db.Column(db.Text, nullable=False)
    img_url = db.Column(db.String(250), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey("users.id"))

    author = relationship("User", back_populates="posts")

db.create_all()

@app.route('/')
def home():
    posts = BlogPost.query.all()
    user = current_user
    return render_template("index.html", all_posts=posts, user=user)

I still don't know what's wrong, but the following code works in a newer project.我仍然不知道出了什么问题,但是以下代码适用于较新的项目。 Like I said in one of the comments: I suspect that there's an issue with my current PyCharm project.就像我在其中一条评论中所说的那样:我怀疑我当前的 PyCharm 项目存在问题。

db = SQLAlchemy(app)

class User(UserMixin, db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(100), unique=True)
    password = db.Column(db.String(100))
    name = db.Column(db.String(100))
    posts = relationship("BlogPost", back_populates="author")


class BlogPost(db.Model):
    __tablename__ = "blog_posts"
    id = db.Column(db.Integer, primary_key=True)
    author_id = db.Column(db.Integer, db.ForeignKey("users.id"))
    author = relationship("User", back_populates="posts")
    title = db.Column(db.String(250), unique=True, nullable=False)
    subtitle = db.Column(db.String(250), nullable=False)
    date = db.Column(db.String(250), nullable=False)
    body = db.Column(db.Text, nullable=False)
    img_url = db.Column(db.String(250), nullable=False)

db.create_all()

@app.route('/')
def home():
    posts = BlogPost.query.all()
    return render_template("index.html", all_posts=posts)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM