简体   繁体   English

在 SQLAlchemy Flask 中创建对象时出现 NoForeignKeysError

[英]NoForeignKeysError when creating object in SQLAlchemy Flask

I am a beginner with SQLAlchemy and I just did my first modules.py file for a Flask application.我是 SQLAlchemy 的初学者,我刚刚为 Flask 应用程序编写了我的第一个 modules.py 文件。 However, in the main app, when I try to create two objects of type user :但是,在主应用程序中,当我尝试创建两个 user 类型的对象时:

from models import user_presence,User,Activity_Presence
db.create_all()
u1 = User()
u2 = User()

I get the error that: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'activity_presence' and 'User_Presence'.我得到的错误是: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'activity_presence' and 'User_Presence'. I tried following the official tutorials, but I don't understand why there is an issue with the foreign key relationship.我尝试按照官方教程进行操作,但我不明白为什么外键关系会出现问题。 I also tried adding more fields, adding objects to the relationship, but I just can't figure out what the problem is.我还尝试添加更多字段,向关系添加对象,但我无法弄清楚问题是什么。 If you have any idea I would be very thankful.如果您有任何想法,我将不胜感激。 Sorry if the question is too much of a beginner one.对不起,如果问题是初学者的问题太多。

from api import db

user_presence = db.Table('User_Presence',
                db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
                db.Column('presence_id', db.Integer, db.ForeignKey('Activity_Presence.id'), primary_key=True)
                )

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    presence_activ= db.relationship('Activity_Presence', secondary=user_presence, lazy='subquery',
                           backref=db.backref('users', lazy=True))

class Activity_Presence(db.Model):
    id = db.Column(db.Integer, primary_key=True)

The error lies in the reference to the wrong table name.错误在于引用了错误的表名。
The usual naming of the classes that represent the model is done in camelcase.代表模型的类的通常命名是以驼峰命名的。 This name is then converted to snakecase to provide the table name.然后将此名称转换为蛇形大小写以提供表名称。 If you should use an underscore in your class name, it will be retained and another one will be added for any subsequent capital letters.如果你应该在你的类名中使用下划线,它将被保留,并且会为任何后续的大写字母添加另一个。

As an an example:举个例子:

class ActivityPresence(db.Model):
    __tablename__ = 'activity_presence'
class Activity_Presence(db.Model):
    __tablename__ = 'activity__presence'

So the working code is as follows.所以工作代码如下。

user_presence = db.Table('user_presence',
    db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
    db.Column('presence_id', db.Integer, db.ForeignKey('activity_presence.id'), primary_key=True)
)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    presence_activ= db.relationship('ActivityPresence',
        secondary=user_presence,
        lazy='subquery',
        backref=db.backref('users', lazy=True))

class ActivityPresence(db.Model):
    id = db.Column(db.Integer, primary_key=True)

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

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