Skip to content

Commit

Permalink
Add preserve_pkey argument which allows user to specify primary key f…
Browse files Browse the repository at this point in the history
…or tables which do not auto populate.
  • Loading branch information
ptomasula committed May 10, 2022
1 parent 32897bc commit 30ed3fa
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/odm2datamodels/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,32 @@ def read_query(self,
return df.to_dict()
raise TypeError("Unknown output format")

def create_object(self, obj:object) -> Union[int, str]:
pkey_name = obj.get_pkey_name()
setattr(obj, pkey_name, None)
def insert_query(self, objs:List[object]) -> None:
with self.session_maker() as session:
session.add_all(objs)
session.commit()

def create_object(self, obj:object, preserve_pkey:bool=False) -> Union[int, str]:
""" Accepts an ORM mapped model and created a corresponding database record
Accepts on one of the ORM mapped models and creates the corresponding database
record, returning the primary key of the newly created record.
Arguments:
obj:object - The ORM mapped model
preserve_pkey:bool - Default=False - flag indicating if the primary key for
the object should be preserved. Avoid in general use cases where database has
a serial that auto assigned primary key, however this can be set to True to
specify you own the primary key value.
Returns:
primary key: Union[int, str]
"""

if not perserve_pkey:
pkey_name = obj.get_pkey_name()
setattr(obj, pkey_name, None)

with self.session_maker() as session:
session.add(obj)
Expand Down

0 comments on commit 30ed3fa

Please sign in to comment.