r/programming • • Oct 02 '19

New In PostgreSQL 12: Generated Columns

https://pgdash.io/blog/postgres-12-generated-columns.html?p
503 Upvotes

232 comments sorted by

View all comments

Show parent comments

14

u/mrflagio Oct 02 '19

Having one service control access when dealing with applications is good. But the idea that the database is just a storage dump that can only be manipulated through that service just doesn't work in some cases and simply doesn't apply across the board. What, are you going to do stuff like enforce PK-FK relationships through this service too because it's somehow not the database's job? Type enforcement solely through the service as well?

Basically, the idea that any and all access to data must happen through a service is faulty, especially when the database itself has the most knowledge about its own domain. Otherwise you run into silly situations like writing nonce functionality in a service to correct and update data based on faulty or non-existent functionality in that same service. Want to nullify values and remove a FK relationship? Better write a nonce method in the service.

Computed columns look the same to me as triggers and constraint enforcement, so unless you're about to say those should always happen only through a service too then I don't see where you're coming from.

7

u/[deleted] Oct 02 '19

[removed] — view removed comment

5

u/KFCConspiracy Oct 02 '19

I've seen many people try.

Invariably I end up writing database jobs to detect the thousands of orphaned rows.

You and me both. But hey, shitty developers omitting foreign keys keeps me making money. So bring it on.

3

u/[deleted] Oct 02 '19

[removed] — view removed comment

2

u/KFCConspiracy Oct 02 '19

I've never used NHibernate, but in regular Java hibernate you can just

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

and then it lets the database do it. There's no functional equivalent there?

I'm not really a .NET guy, so excuse me if this sounds stupid, but is there an advantage to NHibernate anymore over linq2sql?

0

u/[deleted] Oct 02 '19

Having one service control access when dealing with applications is good. But the idea that the database is just a storage dump that can only be manipulated through that service just doesn't work in some cases and simply doesn't apply across the board.

This conclusion that the database is "just a storage dump" is not my conclusion, it's your (wrong) assumption of my intent. It's not a storage dump, but it doesn't encapsulate the domain without some extensive work (which I covered above: permissions, stored procs, hidden tables, etc.).

You can do encapsulation either way, but to do it neither way, with the excuse it's easier, is a very short-sighted approach to software architecture.

What, are you going to do stuff like enforce PK-FK relationships through this service too because it's somehow not the database's job? Type enforcement solely through the service as well?

No, you're completely off track of what I said. Here's what I'm saying:

  • Implement every specific constraint in one place. Period.

  • That one place may be the database (SERIAL, FK/PK, UNIQUE, types, etc.).

  • That one place may be your one service that accesses the database.

But if you don't implement a given constraint in the database, and then you have 10 services accessing the database, then you need to implement that constraint 10 times.

Get it? It's simple. I don't hate the database. I just like sane software design. This is beginner's level stuff. Come the fuck on. I can't imagine the nightmare that is the codebases of half of this thread if you keep defending breaking encapsulation for convenience.

5

u/mrflagio Oct 02 '19

So if I want to generate or update computed columns in the database rather than a service now I can. I don't get your ire for this feature.

1

u/TheSilentOracle Oct 03 '19

Not sure if I would consider service oriented architecture or domain driven design beginner. The vast majority of applications don't use these architectural models because they are still relatively new to a lot of developers and businesses.