在SQLAlchemy中,如何在2个不同的.py文件之间创建一个ForeignKey关系? [英] In SQLAlchemy, how do I create a ForeignKey relationship across 2 different .py files?

查看:503
本文介绍了在SQLAlchemy中,如何在2个不同的.py文件之间创建一个ForeignKey关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

user_models.py 中,我有:

class Users(Base):
    __tablename__ = 'account_users'
    id = Column(Integer, primary_key = True)
    username = Column(String(255), nullable=False)    
Base.metadata.create_all(engine)



当我运行这个,我创建一个用户表。

When I run this, I create a user table.

在我的另一个文件中, groups_models.py ,我有:

On my other file, groups_models.py, I have this:

class Groups(Base):
    __tablename__ = 'personas_groups'
    id = Column(Integer, primary_key = True)
    user_id = Column(Integer, ForeignKey('account_users.id')) #This creates an error!!!
    user = relationship('Users') #this probably won't work. But haven't hit this line yet.

Base.metadata.create_all(engine)

看到,我想要从组 - >用户之间建立多对一的关系。

So, as you can see, I want to put a many-to-one relationship from groups -> users.

但是当我运行 groups_models.py ...我收到此错误:

But when I run groups_models.py...I get this error:

sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'personas_groups.user_id' could not find table 'account_users' with which to generate a foreign key to target column 'id'

如果我把两个表放在一个文件,我相信它可以工作...但因为我把它拆分成2个文件(我绝对必须)...我不知道如何使ForeignKey关系

If I put the two tables together in one file, I'm sure it could work...but because I split it into 2 files (which I absolutely have to)...I don't know how to make ForeignKey relationships work anymore?

basetest.py

解决方案

推荐答案

p>

basetest.py

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import Column, Integer, String
from sqlalchemy import Table, Text

engine = create_engine('mysql://test:test@localhost/test1',
                    echo=False)

Base = declarative_base()

user_models.py

from sqlalchemy import Column, Integer, String
from sqlalchemy import Table, Text


#Base = declarative_base()
from basetest import Base

class Users(Base):
    __tablename__ = 'account_users'
    __table_args__ = {'extend_existing':True}
    id = Column(Integer, primary_key = True)
    username = Column(String(255), nullable=False)    

Base.metadata.create_all(engine)

groups_models.py

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import Column, Integer, String
from sqlalchemy import Table, Text

from basetest import Base
#Base = declarative_base()
from test1 import Users

class Groups(Base):
    __tablename__ = 'personas_groups'
    __table_args__ = {'extend_existing':True}
    id = Column(Integer, primary_key = True )
    user_id = Column(Integer, ForeignKey('account_users2.id')) #This creates an error!!!
    user = relationship(Users) #this probably won't work. But haven't hit this line yet.

Base.metadata.create_all(engine)

Base 创建所有相关表。

这篇关于在SQLAlchemy中,如何在2个不同的.py文件之间创建一个ForeignKey关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆