6 ms·
What ORM do people use that influences the structure of their database? The ORM I'm currently using the most can do whatever I need with my Postgres DB. Also,
by fastball 7y ago
What ORM do people use that influences the structure of their database? The ORM I'm currently using the most can do whatever I need with my Postgres DB.
Also, the ORM allows me to specify models that are not only used for structuring the database, but also for validation of incoming JSON requests and easily serialize queries back to JSON.
- taffer 7y ago> What ORM do people use that influences the structure of their database? Hibernate and JPA encourage designing your domain classes first and then generate the DDL from that. > Also, the ORM allows me to specify models that are not only used for structuring the database, but also for validation of incoming JSON requests and easily serialize queries back to JSON. Postgres has great JSON support, does the ORM something with JSON that Postgres cannot do?
- stickfigure 7y agoHibernate and JPA encourage designing your domain classes first and then generate the DDL from that. This is not true. There's a culture of doing that in demos, but every production shop I've ever been in curates DDL by hand. Flyway is pretty popular.
- shkkmo 7y ago> Hibernate and JPA encourage designing your domain classes first and then generate the DDL from that. This is a feature, not requirement or need of the ORM. It seems pretty silly to let the existence of a feature prevent you from making designing the structure of your DB correctly. > does the ORM something with JSON that Postgres cannot do? Postgres's json functionality is used for manipulating and querying data stored in the DB. I believe the poster is talking about deserializing and validating json from REST requests and serializing json for REST responses using the mapping defined for the ORM.
- taffer 7y ago> I believe the poster is talking about deserializing and validating json from REST requests and serializing json for REST responses using the mapping defined for the ORM. These are also things that the json functionality of Postgres can do. For example, look at to_json and json_agg.
- stickfigure 7y agoIn my current company I use Postgres JSONB with Hibernate extensively. One of the benefits of the ORM is that json fields can be constrained to a fixed schema. For example, a tag list field can be a SortedSet of strings. A couple other things I've learned: * Never re-use complex types in both your API and your schema. These things evolve at different paces and you should never have to worry that a change to your schema will break an API (or vice-versa). The minimal extra typing to have dedicated API types is well worth it. * Storing untrusted client-submitted JSON in your database is a terrible idea. This is a great attack surface, either by DOSing your system with large blobs or by guessing keys that might have meaning in the future.
- fuzz4lyfe 7y ago> incoming JSON requests import json data_from_json_request = json.loads(*request body*) > easily serialize queries back to JSON import pymysql import json conn = pymysql.connect(*connection details*, cursorclass = pymysql.cursors.DictCursor) cur = conn.cursor() cur.execute(*query*) json_query_result = json.dumps(cur.fetchall()) I don't feel that a ORM is better than this personally. I know exactly what this is doing at all times. No magic, no guess work about the philosophy of the software. This is probably faster as well.
- fastball 7y agoSorry, maybe I wasn't being clear. I don't just validate that the request is JSON, I validate that the fields in the JSON are valid fields to send over the wire. I do this by automatically hooking my ORM models into my request validation. If a request doesn't specify a column that is NOT-NULL, for example, it will automatically send an error response telling the client that it needs to specify that column in the request JSON.
- fuzz4lyfe 7y agoPython doesn't have type safety to begin so those sort of checks have less utility to me and since json.loads returns a dictionary and python objects are effectively dictionaries you are pretty much done.
- dragonwriter 7y ago> Python doesn't have type safety It does, if you want it to, with several typecheckers available.
- fuzz4lyfe 7y agoType annotations were recently introduced as a standard library feature as well though they aren't enforced and require a third party tool like you are describing. I've found that 90% of the time you don't actually care what type something actually is and instead you care what it needs to be.
- aryik 7y agoIs it ActiveRecord? I find it to be very flexible and convenient for 95% of my use cases. There are a few places where we drop to raw SQL but AR makes that very easy.
- sjwright 7y agoORMs that I've experimented with tend to fall into one of two categories: either they treat the object model as prime, or they treat the relational model as prime. The former almost invariably spurt out inefficient queries, or too many queries, or both. They usually require you to let the ORM generate tables. If you just want to have your object oriented design persist in a database, that's great. The latter almost invariably results in trying to reinvent the SQL syntax in a quasi-language-native, quasi-database-agnostic way. They almost never manage to replicate more than a quarter of the power of real SQL, and in order to do anything non-trivial (or have things done in a way that lets your database server scale) they force you to become an expert SQL anyway, PLUS an expert in how your ORM translates its own syntax into SQL. And once you become more expert at SQL than your ORM, it's not long before you find the ORM is a net loss to productivity—in particular by how it encourages you to write too much data manipulation logic in code rather than directly in the database.
- shkkmo 7y agoI think you may have only experienced bad ORMs then? All an ORM needs is a mapping between database fields and object properties so a good ORM should allow you to separately define a mapping between your object model and relational model so you retain full control of both. > it encourages you to write too much data manipulation logic in code rather than directly in the database I find doing too much business logic related data manipulation directly via SQL to be an anti-pattern that creates significant problems with testing and separation of concerns. ORMs are good at hydrating objects and persisting updates to those objects. Hand writing code to do this is a waste of time. SQL is good at running reports and performing mass updates an ORM that doesn't allow you to easily do this is bad.
- sjwright 7y agoWhereas I find doing too much business logic related data manipulation not performed by the database to be an anti-pattern that creates significant risks with testing and a source of data bugs. My model of thinking is that any copy of data that isn't currently resting in the database is potentially stale; avoid round trips like the plague; get new data into the database as soon as possible. For me and the way I work, it's less about good vs bad ORMs, rather more often a question of whether I even want my data hydrated into a special object at all. I've come to the realisation that for the kind of work I do, data objects almost always end up being an unnecessary layer of indirection that don't give me any real benefits—and they change the way you think, because every transform becomes an opportunity to write a method on an object and not a straightforward query.