Dear all,

I am quite new to ORMs and SQLAlchemy, and I have a maybe somewhat naive 
question set regarding how to build an application around a database.

Just a few words of context (may not yield importance, but who knows): I am 
building a mobile app, for which the server side will be an AWS Lambda 
function serving GraphQL queries. Data persistence is achieved through a 
hosted PostgreSQL instance. Server side code is python and "database 
access" through SQLAlchemy. The first version of the database schema has 
been "manually" built and populated with some test data (via simple SQL 
queries in pgAdmin4).

Regarding SQLAlchemy usage, first version was using Core only, but I 
decided to move to ORM, and I got it quite hard - maybe because of poor 
choices on my end.

What I do, now that I have a working example:
- when the Lambda is fired, I import a module defining "bare" ORM classes, 
with no attribute apart the table name - inheriting from *`Reflected`* and 
a declarative base (using deferred reflection 
<https://docs.sqlalchemy.org/en/20/orm/declarative_tables.html#using-deferredreflection>
)
- engine is set up and connection established to the db
- *`Reflected`* class is prepared through *Reflected.prepare(engine=engine)*
- New attributes, relationships, etc... are added to the "bare" ORM classes 
as *`column_property`*, after reflection (this way 
<https://docs.sqlalchemy.org/en/20/orm/mapped_sql_expr.html#adding-column-property-to-an-existing-declarative-mapped-class>
)
- Mapper is then ready, data is queried and mutated using sessions

My questions are:
- is reflection mandatory when working with an existing database? (I think 
this would be like an overwhelmingly prevalent case for Production 
applications?)
- is it possible to have a mixed approach regarding the mapping 
definitions: some attributes being defined in the ORM classes prior to the 
reflection, and reflection then completes those classes with other fields 
from the database schema?
- when using reflection, is the only way to define new attributes, 
relationships, etc... to add those attributes after this reflection via 
adding column_properties after class definition, like described above?
- I feel like I am losing much of the "Declarative Mapping" by working the 
way I do, what do you think about it?
- overall, what could be simplified regarding the ways of working I set up?

Some code snippets below:

*Bare ORM class definitions:*
class MessageOrm(Reflected, Base):
__tablename__ = "single_recipient_message"


class HeaderOrm(Reflected, Base):
__tablename__ = "single_recipient_message_header"

*Post reflection addition of relationships and attributes*
HeaderOrm.messages = relationship(
MessageOrm,
foreign_keys=[MessageOrm.header_id],
back_populates="header",
)
MessageOrm.sent_by_who = column_property(
case(
(MessageOrm.sender_id == current_id, "me"),
else_="other",
)
)
MessageOrm.header = relationship(
"HeaderOrm",
foreign_keys=[MessageOrm.header_id],
back_populates="messages",
)

Thanks a lot!

Regards,

Pierre





-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/433f9917-7ac0-4d1b-b41e-16d8ae255d15n%40googlegroups.com.

Reply via email to