在单独的文件 FLASK、SQLalchemy 中创建模型之间的关系
Creating relationship between models when in separate files FLASK, SQLalchemy
当这两个模型当前位于不同的文件夹中时,如何在它们之间正确建立外键关系?
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey
from testserver.database import Base
from datetime import datetime
class Service(Base):
__tablename__ = 'services'
id = Column(Integer(), primary_key=True)
name = Column(String(25), nullable=False, unique=True)
description = Column(String(80), nullable=False)
date_created = Column(DateTime(), default=datetime.utcnow)
provider_id = Column(String, ForeignKey('provider.id'))
from sqlalchemy import Column, String, Integer, DateTime
from sqlalchemy.orm import relationship
from testserver.database import Base
from datetime import datetime
from testserver.database import db_session
class Provider(Base):
__tablename__ = 'providers'
id = Column(Integer(), primary_key=True)
name = Column(String(25), nullable=False, unique=True)
description = Column(String(80), nullable=False)
date_created = Column(DateTime(), default=datetime.utcnow)
services = relationship(
'Service', cascade='all, delete-orphan', backref='provided_by')
from sqlalchemy.ext.declarative import declared_attr
class Base(object):
__abstract__ = True
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
上面的代码目前抛出一个错误:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'services.provider_id' could not find table 'provider' with which to generate a foreign key to target column 'id'
如有任何建议或解决方案,我们将不胜感激
我假设你这里有错字:
provider_id = Column(String, ForeignKey('provider.id'))
在关系中,您提供了小写的模型名称,而不是 table 名称。如果您将 provider
替换为 providers
(因为它是您的 table 的名称)它应该可以解决您的问题
provider_id = Column(String, ForeignKey('providers.id'))
当这两个模型当前位于不同的文件夹中时,如何在它们之间正确建立外键关系?
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey
from testserver.database import Base
from datetime import datetime
class Service(Base):
__tablename__ = 'services'
id = Column(Integer(), primary_key=True)
name = Column(String(25), nullable=False, unique=True)
description = Column(String(80), nullable=False)
date_created = Column(DateTime(), default=datetime.utcnow)
provider_id = Column(String, ForeignKey('provider.id'))
from sqlalchemy import Column, String, Integer, DateTime
from sqlalchemy.orm import relationship
from testserver.database import Base
from datetime import datetime
from testserver.database import db_session
class Provider(Base):
__tablename__ = 'providers'
id = Column(Integer(), primary_key=True)
name = Column(String(25), nullable=False, unique=True)
description = Column(String(80), nullable=False)
date_created = Column(DateTime(), default=datetime.utcnow)
services = relationship(
'Service', cascade='all, delete-orphan', backref='provided_by')
from sqlalchemy.ext.declarative import declared_attr
class Base(object):
__abstract__ = True
@declared_attr
def __tablename__(cls):
return cls.__name__.lower()
上面的代码目前抛出一个错误:
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'services.provider_id' could not find table 'provider' with which to generate a foreign key to target column 'id'
如有任何建议或解决方案,我们将不胜感激
我假设你这里有错字:
provider_id = Column(String, ForeignKey('provider.id'))
在关系中,您提供了小写的模型名称,而不是 table 名称。如果您将 provider
替换为 providers
(因为它是您的 table 的名称)它应该可以解决您的问题
provider_id = Column(String, ForeignKey('providers.id'))