Skip to content

Commit

Permalink
fix: save new sources on bulk save (#186)
Browse files Browse the repository at this point in the history
Signed-off-by: F.N. Claessen <[email protected]>
  • Loading branch information
Flix6x authored Aug 30, 2024
1 parent 60a2793 commit 93ef8b1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion timely_beliefs/beliefs/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,23 @@ def add_to_session(
session.expunge_all()

if bulk_save_objects:
# serialize source and sensor
# serialize sources and sensor, while adding new sources

# serialize sources
beliefs_data_frame["source_id"] = beliefs_data_frame["source"].apply(
lambda x: x.id
)

# Add new sources
newbies = pd.isnull(beliefs_data_frame["source_id"])
if any(newbies):
session.add_all(beliefs_data_frame.loc[newbies, "source"])
session.flush() # assign IDs
beliefs_data_frame.loc[newbies, "source_id"] = beliefs_data_frame.loc[
newbies, "source"
].apply(lambda x: x.id)

# serialize sensor
beliefs_data_frame["sensor_id"] = beliefs_data_frame.sensor.id
beliefs_data_frame = beliefs_data_frame.drop(columns=["source"])

Expand Down

0 comments on commit 93ef8b1

Please sign in to comment.