I'm trying to connect my FastAPI project to 2 databases, one is gonna be local and the other one is located in an IP address. I think I managed to connect to both. However, when I try to create the local db, I get the file of the file, but is not pulling the models that I created for that db.
my code for database.py is the following:
SQLALCHEMY_DATABASE_URL = "credentials and address"
AUTH_DATABASE_URL = "sqlite:///./users.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
engine2 = create_engine(AUTH_DATABASE_URL, connect_args={
"check_same_thread": False})
Base = declarative_base()
BaseB = declarative_base()
SessionLocal = sessionmaker(autocommit=False, autoflush=False)
SessionLocal.configure(binds={Base: engine, BaseB: engine2})
The models I created for that base are the following:
class User(BaseB):
__tablename__ = "users"
Id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)
##Constraints##
leads = relationship("Lead", back_populates="owner")
def verify_password(self, password: str):
return hash.bcrypt.verify(password, self.hashed_password)
class Lead(BaseB):
__tablename__ = "leads"
Id = Column(Integer, primary_key=True, index=True)
owner_id = Column(Integer, ForeignKey("users.Id"))
first_name = Column(String, index=True)
last_name = Column(String, index=True)
email = Column(String, index=True)
company = Column(String, index=True, default="")
notes = Column(String, default="")
date_created = Column(String, default=datetime.utcnow)
date_last_update = Column(String, default=datetime.utcnow)
##Constraints##
owner = relationship("User", back_populates="leads")
Also, I created a services.py file, which I use to run the code to create the database. It's creating the users.db file, but when I try to open it, I don't get the tables I created:
from database import BaseB, engine2, SessionLocal
def create_database():
return BaseB.metadata.create_all(bind=engine2)
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
This is causing me problems, because I need to post data to the table users. However, when I try to do it, I get an error saying there is no such table created. I've tried even to run the code to generate the db automatically (without running services.py on python) but still I get the same error.
I would appreciate if someone can take a look at my code and check what I may be missing.
Thanks!!
Thank you!
I think I have figured out what was wrong. I post this solution in case anyone else might get trapped as I was. My error was that I wasn't importing the models, neither declaring it in the services.py route. I changed my code and now I can see the two tables when I create the database from python
from database import engine2, SessionLocal import models def create_database(): return models.BaseB.metadata.create_all(bind=engine2)
Hope it helps anyone having the same problem :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.