我对FASTAPI比较陌生,但我决定用Postgres和Alembic建立一个项目。每次我使用自动迁移时,我都设法让迁移创建新版本,但由于某种原因,我没有从我的模型中得到任何更新,唉,它们保持空白。我有点不知道出了什么问题。
Main.py
from fastapi import FastAPI
import os
app = FastAPI()
@app.get("/")
async def root():
return {"message": os.getenv("SQLALCHEMY_DATABASE_URL")}
@app.get("/hello/{name}")
async def say_hello(name: str):
return {"message": f"Hello {name}"}
Database.py
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
SQLALCHEMY_DATABASE_URL = os.getenv("SQLALCHEMY_DATABASE_URL")
engine = create_engine("postgresql://postgres:mysuperpassword@localhost/rodney")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
except:
db.close()
目前为止我唯一的模特
from sqlalchemy import Integer, String
from sqlalchemy.sql.schema import Column
from ..db.database import Base
class CounterParty(Base):
__tablename__ = "Counterparty"
id = Column(Integer, primary_key=True)
Name = Column(String, nullable=False)
env.py (alembic)
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
from app.db.database import Base
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
现在,当我运行“alembic revision --autogenerate -m“初始设置””x1c 0d1x“时,Alembic会创建ampty迁移
我的文件夹结构
如果有人知道,我会非常感谢的,干杯!
3条答案
按热度按时间uttx8gqw1#
在我的案例中,我使用Transformer BERT模型部署在FastApi上,但fastapi无法识别我的模型,也无法获取模型输入和输出。我用于案例的代码:
上面的代码使用pydantic中的BaseModel,我为baseModel创建了类,以获取
text:str as input
和headings, Probability, and prediction as Outputs in EntitiesOut class
之后,模型以某种方式识别了它,并保存200状态代码和输出46scxncf2#
env.py文件找不到模型,因为您还没有导入它们。一个解决方案是,您只需将它们立即导入到env.py文件中,如下所示:
从..模型导入 *
但是,您需要在models目录中有一个init.py文件,并在其中包含所有模型。
另一种方法(但不推荐):如果您只有一个模型,您可以直接将其导入为:
从..模型.交易方模型导入
0dxa2lsx3#
这是一个有点晚的回应,但我刚刚遇到同样的问题,也许我的答案会帮助别人在未来:)在我的情况下,这是由于数据库状态-它已经在一致的状态,这意味着alembic没有试图创建它的修改(它没有看到任何差异)。当我使用sqlite我只是删除sqlite文件(删除表应该工作),并再次运行
revision
。这一次,它的工作与预期一样,upgrade
和downgrade
函数填充自动生成的代码。