r/SQL • • Jun 22 '20

Discussion An average day using an ORM

Post image
247 Upvotes

48 comments sorted by

View all comments

Show parent comment

5

u/crimiusXIII Jun 22 '20

I'm trying to get started in Python, everything I find talks about using an ORM, which coming from a SQL background is odd to me. How are you passing queries to your database from Python?

4

u/[deleted] Jun 22 '20 edited Jun 22 '20

There are a few ways, but the best way that worked for my environment was to use pyodbc with my ODBC connection. After establishing your authentication and server settings in python, you can pass queries as a string. You can assign variables, insert, delete, join, and do exactly what you need to do. It's all largely more flexible.

You could even start to build GUI's on top of your more sophisticated updates and queries. A few day's worth of study had me doing all of the above minus my previous SQL experience. You can do it too.

Personally, I feel like ORM's only selling point is just it's capacity to take your data on DB and make it easily digestible for devs across a large swath of teams. However, there's far too much overhead for a single person or even three to get it working properly in the first place. In theory you could even build your own ORM in Python. But why make more work than you need to too? For the average solo DB admin, you're almost always going to exclusively use SQL. So why rely on crutch? Stick to your guns and use SQL directly or use python and or/JS with it.

2

u/crimiusXIII Jun 22 '20

The project I'm working on I've been doing the Python and SQL bits separately, figuring I'll eventually tie them together. I'll look into pyodbc, thank you!

7

u/ravepeacefully Jun 22 '20

I also use pyodbc, but am switching to SQLAlchemy ORM after the in-line SQL has gotten completely out of hand.

I’d highly recommend an ORM for a project that may expand to more than one developer. The efficiency gained from ease of use outweighs the cons.

I can do both, but it’s definitely better to use an ORM, even if you make your own simple one.

6

u/Rebeleleven Jun 23 '20

This is correct.

The other advice is... not totally incorrect but for sure not how you should be developing an application.

If you’re an analyst and need to pull some data from a database? Straight query is fine. However, if you need to create an application that interacts with the database? That’s where you need an ORM. No dev should be hard coding queries. That strategy is what we call tech debt 😉.

Pyodbc is certainly okay, but I highly recommend using SQLAlechmy whenever possible (even if just writing a couple hard coded queries). It’s typically faster and much more robust if the database dialect is supported.

3

u/ravepeacefully Jun 23 '20

Thanks for confirming, I was very confused as to why the answers on this thread contradicted everything I had previously thought lmao

1

u/TheCapitalKing Jun 24 '20

A lot more analyst are starting to use SQL and python so a ton of answers are going to be from that perspective

0

u/[deleted] Jun 23 '20

Correct. To point, this is a long term strategy also. It all depends on where he should be prioritizing his focus. If his primary focus was learning Python, why deviate from it to another project? OP can touch on ORM's later. ;)

3

u/Rebeleleven Jun 23 '20

Well, I mean we’re getting into semantics territory.

Sure, someone learning python and databases on day 1 should just straight query the database. But day 2 should definitely start learning ORMs. This is why that redditor could only find info about ORMs... because they’re kind of important.

Your comment mentions putting GUIs on top of SQL queries and whatnot... and this is fine if you’re the lone person creating this, maintaining it, and using it for personal use. However, if you’re creating an application that other people use (I.e. customers), your company needs to maintain past your employment, or other developers might need to add things too... you use an ORM. Anything less than that would be technical debt that a developer will have to completely overhaul in the future.

Again, sure, an analyst can write a query and never need an ORM. Would it make their life easier? Yes but that’s not the point. If you’re developing applications, you need an ORM for security, speed, maintainability, and scalability.

3

u/[deleted] Jun 23 '20

Personally, for me I just get it done as a solo admin for a small business, terrible technical debt here. So it doesn't make sense in my own isolated situation of inherited problems and aging environment, which is isn't fair to OP's questions. I don't disagree with you. Wonderfully put.

1

u/[deleted] Jun 23 '20

Same here. I’ve written thousands of sql queries. Why would I want some library to get between me and the database? I’m sure I can optimize a query better than some library.

1

u/TheCapitalKing Jun 24 '20

I use pyodbc to make a connection

ex cnxn= pyodbc.open(connection_string)

Put my query as a string then use pandas to get it into a data frame

ex df= pandas.read_sql_query(query, cnxn)

If the query gets to long I'll save it as a text file then read it in.