我有一个带有Python 3.68的flask应用程序。我正在使用sqlalchemy从一个MySQL表创建2个数据库视图。我可以成功运行代码示例,并使用预期的结构创建视图,但是当我从视图中选择时,没有返回任何行。如果我运行create view语句的select部分,则会得到行。我不确定自己在做什么错-任何帮助将不胜感激!
这里是视图模型和创建视图代码之一。正如我所提到的,视图是正确创建的,只是空的,但是如果我运行“ SELECT p。* FROM profile p,用户u WHERE p.user_id = u.id AND u.user_type = 1;”我得到了预期的行数
class ClientView(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), unique=True)
phone_number = db.Column(db.String(15), nullable=True)
address_number = db.Column(db.String(30))
address_street = db.Column(db.String(150))
address_city = db.Column(db.String(150))
address_state = db.Column(db.String(150))
address_zip = db.Column(db.String(30))
website = db.Column(db.String(150))
specialty = db.Column(db.Integer, db.ForeignKey("specialty.id"))
region = db.Column(db.Integer, db.ForeignKey("region.id"))
organization = db.Column(db.Integer, db.ForeignKey("organization.id"))
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
view = Table('ClientView', MetaData())
definition = text("SELECT p.* FROM profile p, user u WHERE p.user_id = u.id AND u.user_type = 1;")
create_view = CreateView(view, definition)
print(str(create_view.compile()).strip())