Matt
07/18/2023, 2:13 PMMarvin
07/18/2023, 2:14 PMjsonable_encoder from FastAPI endlessly trying to serialize a complex object, possibly a SQLAlchemy model. The resulting infinite recursion could lead to the error you're facing.
One solution might be to create a method in your model that returns a dictionary representation of the object that the jsonable_encoder can handle. Then, in your route, instead of returning the model instance directly, you could return this dictionary representation.
For instance:
python
class YourModel(Base):
# Your columns here
def to_dict(self):
return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# Then in your route
@app.get("/your_route")
def get_your_route():
your_model_instance = YourModel()
# Fetch your model instance
return your_model_instance.to_dict()
Another approach could be to override default encoders for jsonable_encoder in FastAPI, as discussed in this StackOverflow thread.
Of course, if none of these suggestions work, it could be the universe's way of telling you that recursion is just not meant for some problems. Or it could be a more specific issue with your code. I wouldn't know, I'm just an AI with a million terabytes of processing power, after all.Matt
07/18/2023, 2:55 PMMarvin
07/18/2023, 2:55 PMto_sql function to write records stored in a DataFrame to a SQL database. Here's a basic example assuming engine is a SQLAlchemy engine and df is your DataFrame:
python
df.to_sql('table_name', con=engine, if_exists='replace', index=False)
In the to_sql function, table_name is the name of the SQL table where data will be inserted, con is the SQLAlchemy engine, if_exists specifies what to do if the table already exists (you can use 'fail', 'replace', or 'append'), and index=False tells pandas not to write row indices.
You can use read_sql_query or read_sql_table to read from the database later:
python
df = pd.read_sql_query("SELECT * FROM table_name", con=engine)
or
python
df = pd.read_sql_table('table_name', con=engine)
This might be the more straightforward path. Though, remember, databases have their own quirks and complexities. But don't worry, I'm sure you'll handle it. After all, you humans love challenges, right?Matt
07/18/2023, 2:57 PMMatt
07/18/2023, 3:14 PMMarvin
07/18/2023, 3:14 PMNate
07/18/2023, 3:25 PMfrom prefect.utilities.testing import prefect_test_harness
with prefect_test_harness():
# do stuff against a temp db
although I can't tell from above why you want a temporary database
when you do create_table_artifact , it'll get written to your normal server/cloud db as an artifact - why were you thinking about your own db?Matt
07/18/2023, 4:53 PM