Re: SQLAlchemy: When to initialise a session

2022-02-08 Thread Loris Bennett
"Loris Bennett"  writes:

> Hi,
>
> I am writing a fairly simple command-line application which will just
> add or delete an entry in a database and then generate a corresponding
> email.
>
> I am using SQLAlchemy to wrap a class around a database and have
>
>   class DatebaseWrapper():
>   """Encapsulation of the database"""
>
>   def __init__(self, url):
>   self.engine = create_engine(url)
>  
> Should I extend the initialisation to
>
>   def __init__(self, url):
>   self.engine = create_engine(url)
>   self.session = sessionmaker(self.engine)
>
> since each there will be only one session per call of the program?
>
> Or, since I am writing the database wrapper as its own module for
> possible reuse, should the program using the wrapper class
> initialise the session itself?

Turns out this is all explained here:

  
https://docs.sqlalchemy.org/en/14/orm/session_basics.html#session-frequently-asked-questions

Sorry for the noise.

-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list


SQLAlchemy: When to initialise a session

2022-02-08 Thread Loris Bennett
Hi,

I am writing a fairly simple command-line application which will just
add or delete an entry in a database and then generate a corresponding
email.

I am using SQLAlchemy to wrap a class around a database and have

  class DatebaseWrapper():
  """Encapsulation of the database"""

  def __init__(self, url):
  self.engine = create_engine(url)
 
Should I extend the initialisation to

  def __init__(self, url):
  self.engine = create_engine(url)
  self.session = sessionmaker(self.engine)

since each there will be only one session per call of the program?

Or, since I am writing the database wrapper as its own module for
possible reuse, should the program using the wrapper class
initialise the session itself?

Cheers,

Loris
 
-- 
This signature is currently under construction.
-- 
https://mail.python.org/mailman/listinfo/python-list