preparing for the workshop
play

Preparing for the workshop Getting & installing the code git - PowerPoint PPT Presentation

Preparing for the workshop Getting & installing the code git clone https://github.com/bustawin/sqlalchemy-tutorial cd sqlalchemy-tutorial Read the README.rst of the project for more instructions. psst. I have a pendrive with Postgres and Git


  1. Preparing for the workshop Getting & installing the code git clone https://github.com/bustawin/sqlalchemy-tutorial cd sqlalchemy-tutorial Read the README.rst of the project for more instructions. psst. I have a pendrive with Postgres and Git installers (Mac, Windows). Any problems? Come talk to me :-) (Yes, you can create a virtualenv / use docker’s psql) 1

  2. SQLAlchemy, from 0 to proficient Xavier Bustamante Talavera bustawin.com — xavier@bustawin.com 2

  3. Objectives • Learn how to use SQLA — in the real WWW world. • Understand and have a reference when dealing with more complicated SQLA constructs. 3

  4. G UERRILLA TUTORIAL 4

  5. Xavier Bustamante • bustawin.com — xavier@bustawin.com • Software engineer / ex-CPO startup. bustawin.com/about/ • Looking for new challenges 😊 5

  6. SQLAlchemy SQL the pythonic way. Special thanks to Michael Bayer, the creator of SQLA & The rest of the community of SQLA & Konsta Vesterinen, the creator of SQLA-utils 6

  7. What I like the most of SQLA • DRY: • Common functionality —not reinvent the wheel. • Relationships (i.e. foreign keys) auto fetched and updated by assigning, adding… to the model. • Declarative pattern; you tell what you want, let SQLA figure the rest. • Right amount of configuration vs coding. Elegantly extend / circumvent SQLA when you just need that thing custom made. • Not magic, but defaults that you can modify / extend. • Don’t over automate / optimize everything. • Abstract from different SQL dialects.* 7

  8. Accessing the documentation • Probably a user of SQLA had your problem before. • Documentation: • Seriously, it is very good: https://docs.sqlalchemy.org/en/13/ • Just use something like Dash. • For help, Stackoverflow (#sqlalchemy) / official Google Groups. • Understand the why / how with the official video tutorial. • Another nice tutorial. 8

  9. Mini introduction Table, columns… Connect DB Write SQL as python http://aosabook.org/en/sqlalchemy.html 9

  10. A0: Intro to SQLA Core 10

  11. A1: Intro to SQLA ORM + Flask What… what is an ORM? 11

  12. https://www.youtube.com/watch?v=woKYyhLCcnU 12

  13. The session • In Flask, the session lives only in the request.* • Any changes you do inside in a request, is by default in the same transaction. • Identity map: unique object per PK. • Querying twice Computer ID 1 in the same session will give you the same python object: pc1 = Computer.query.first() pc1.foo = ‘bar’ pc2 = Computer.query.first() pc1 is pc2 pc2.foo == ‘bar’ • You can safely modify any copy of the same row in your request! http://docs.sqlalchemy.org/en/13/orm/session_basics.html 13

  14. Some more expressions • and_, or_, is_, in_… • or_(Computer.id == 1, Computer.id == 2, Computer.id == 9) • or_(*[Computer.id == id for id in [1,2,9]]) • Computer.id.in_([1,2,9]) • Computer.model + ‘ 2019’ => computer.model || “ 2019” https://docs.sqlalchemy.org/en/13/core/sqlelement.html 14

  15. Break! 15

  16. Execute “sh init-db.sh” between the following examples. 16

  17. B0: Basic relationship http://docs.sqlalchemy.org/en/13/orm/extensions/declarative/relationships.html 17

  18. How would you add the user TO the computer? 18

  19. B1: Many — Many relationship http://docs.sqlalchemy.org/en/13/orm/extensions/declarative/relationships.html#declarative-many-to-many 19

  20. Query wonders • Chain: query methods usually return a new query object. query = Computer.query.filter(…) grouped_query = query.group_by(…) • SQL statements: query.insert / query.delete / query.join … • Configure Lazy Loading / Eager Loading to optimize the number of SQL queries SQLA when loading relationships: query.options(selectinload(User.addresses)) for pc in Computer.query: pc.author # This does SELECT author for each computer • Alias: “ FROM computer c1, computer c2 ” • Filter by: Computer .filter_by(id = 1) == Computer.filter(Computer.id == 1) • Flask’s session: .paginate() .one_or_404() https://docs.sqlalchemy.org/en/13/orm/tutorial.html#querying 20

  21. C: Inheritance (Joined Table Inheritance) http://docs.sqlalchemy.org/en/13/orm/extensions/declarative/inheritance.html 21

  22. D: Types Doc: http://docs.sqlalchemy.org/en/13/core/type_basics.html SQLA-utils: https://sqlalchemy-utils.readthedocs.io/en/latest/data_types.html 22

  23. E: Validations http://docs.sqlalchemy.org/en/13/orm/mapped_attributes.html#simple-validators 23

  24. F: Events http://docs.sqlalchemy.org/en/13/core/event.html 24

  25. How to remove the responsible when is removed as a traveler? 25

  26. H: Natural Search http://docs.sqlalchemy.org/en/13/dialects/postgresql.html#postgresql-match 26

  27. G: Views 27

  28. What have I not explained? • Why things works / architectural and pattern decisions. • Joins in detail. • Properties and hybrid attributes. If I have in a table start_time and end_time , how can I auto-compute an elapsed field? • Reflection: generate the schema (Table()) from the database. • Alembic: database migrations. • GeoAlchemy. 28

  29. Thank you! xavier@bustawin.com bustawin.com 29

Recommend


More recommend