ebean icon indicating copy to clipboard operation
ebean copied to clipboard

Add an optional config to disable foreign key's generation when generate table create sentence

Open archer-christ opened this issue 10 years ago • 11 comments

Sometimes we do not want to gen foregin key in create table sentence , can you provide an optional config to ServerConfig to support this feature, thanks!

archer-christ avatar Apr 08 '16 03:04 archer-christ

Is that globally for all tables ... or just some tables?

BTW: Under what conditions do you not want foreign keys?

Thanks, Rob.

rbygrave avatar Apr 08 '16 03:04 rbygrave

It's a global config for all tables

we just doesn't use foreign keys CONSTRAINTS , Logically there are foreign keys.

see more from http://stackoverflow.com/questions/83147/whats-wrong-with-foreign-keys

Thanks

archer-christ avatar Apr 08 '16 04:04 archer-christ

Which comments actually support not actually having the constraints applied though ... I didn't see any.

For me this is just a really really bad idea (I've still got some old school DBA in my bones obviously). There is only one reason in my book and that sure doesn't mean no foreign key constraints on all tables.

You are asking me to do something that in my view promotes people to do "a really bad thing". So no, if you want me to do this then you are going to have provide a much better case with specific reasons.

Cheers, Rob.

On 8 April 2016 at 16:03, EChrist [email protected] wrote:

It's a global config for all tables

we just doesn't use foreign keys CONSTRAINTS , Logically there are foreign keys.

see more from http://stackoverflow.com/questions/83147/whats-wrong-with-foreign-keys

Thanks

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/ebean-orm/avaje-ebeanorm/issues/644#issuecomment-207199693

rbygrave avatar Apr 08 '16 04:04 rbygrave

Sorry , English is not my mother language , so it's hard to explain very clearly

I got an answer form stackoverflow which close to what I want to say: http://stackoverflow.com/questions/18717/are-foreign-keys-really-necessary-in-a-database-design/19510#19510

Foreign keys had never been explicit (FOREIGN KEY REFERENCES table(column)) declared in projects (business applications and social networking websites) which I worked on.

But there always was a kind of convention of naming columns which were foreign keys.

It's like with database normalization -- you have to know what are you doing and what are consequence of that (mainly performance).

I am aware of advantages of foreign keys (data integrity, index for foreign key column, tools aware of database schema), but also I am afraid of using foreign keys as general rule.

Also various database engines could serve foreign keys in a different way, which could lead to subtle bugs during migration.

Removing all orders and invoices of deleted client with ON DELETE CASCADE is the perfect example of nice looking, but wrong designed, database schema.

http://stackoverflow.com/questions/8754655/should-i-really-use-foreign-keys

There's actually a growing move away from foreign key based relational databases at the moment. Document Database systems such a MongoDB are becoming much more popular. This is largely because the world is becoming much more distributed with the cloud.

This means that sometime not reasonable or performant to assume immediate data consistency.

Have a read on NoSQL based database, MongoDB CouchDB and evential consistancy if you are interested.

Its kinda a weird thing for our relational trained minds to understand but a lot of large scale web solutions

Suppose there is a Blog system, we have blog blog_comment blog_img ... , if using ON DELETE CASCADE we must delete blog_comment blog_img... first then blog

For performance reason I can delete blog first then delete blog_comment blog_img... later via timed task .

In these scenario data consistency may be not the very first thing.

Maybe it's not promotes people to do "a really bad thing". it's just another choice.

archer-christ avatar Apr 08 '16 06:04 archer-christ

Your English is excellent. I'm heading off to bed now ... but I'll get back to this sometime over the weekend.

Cheers, Rob.

rbygrave avatar Apr 08 '16 10:04 rbygrave

Ok here goes:

Lets try to be "Scientific" and theorem / proof based

So this is a general statement from me about IT but for this issues databases and FK's. Over the years of seen quite a few statements that go somethings like "this feature XYZ does not work in RDBMS" ... only to find the only RDBMS they have used in some fairly simple DB like MySql 2.x or SQL Server 2000 etc.

If people have only used a relatively crappy/unsophisticated database then they are often quite happy to suggest these issues they hit apply generally to ALL RDBMS. For me RDBMS implementations differ significantly in architecture (MVCC vs Locking) and quality (Cost based query optimiser, types supports, indexes supported ...).

If you've only used toy databases ...

Rob's bias - good databases

For most of my earlier career I used/saw Oracle and in more recent years Postgres and MySQL. I have used some poor databases and mostly that was SQL Server 2000 (lock based, non-mvcc, large batch processing requirement).

Specifically for me I have not experienced or heard of a an issue with FK constraint enforcement in the DB. Maybe because I have always been exposed to good database implementations.

Data corruption - painful to fix

I myself have seen the other side of db constraints not enforced which is a project that has to "fix corrupt production data". I'd suggest this is a relatively painful, laborious task (I was not directly involved so only saw it 2nd hand). You want to think very hard to see if you want to be responsible for the decision not to have constraints enforced.

Yes the "pain" is often not felt until 10+ years down the track but I'd class it as "significant pain" to the enterprise and almost certainly the top level of management (CIO, CFO etc) are all aware.

stackoverflow - "... but also I am afraid of using foreign keys as general rule."

That statement from stackoverflow is a "feeling" with no associated theory or experience. As such that statement FAILS any sort of scientific testing and hence for me holds no weight.

If the statement was backed up by some experience or theory we could test it to find conditions under which it could be true but no - it is just a FEELING.

ON DELETE CASCADE - is a choice

On delete cascade is a CHOICE made per constraint. You can cascade deletes or not ... and because it is a choice you can not argue for it as a reason not to have foreign key constraints.

..."For performance reason I can delete blog first"

Yes, but you could instead logically delete it and then in background delete both in the order that satisfies the constraint. In general deletes ought to relatively less common and less important. If this is a real issue then I'd expect to give up that one specific constraint - you don't give up all constraints across the entire DB for this reason (just give up the one constraint if you really must).

Q: So when would Rob not use DB FK constraints?

I'm comfortable not having constraints on what I'd describe as interface tables. These are tables that sit between the database schema and some other system like a 3rd party packaged software.

These "interface tables" are somewhat glorified csv files. They hold data that is going to go through some business process in order to get imported into the actual tables.

These tables are not really part of the OLTP database schema but support a mechanism / business process that imports data (and you can have them for exporting data out of the system as well).

"It's just another choice."

Yes but I don't want to give people a choice that I don't see any merit in and which I consider highly dangerous. Currently I see no reasonable conditions where I consider this a good option. My current personal opinion is that this is actually a really bad idea that has no justifying use case.

Caveat

I realise I have my own bias. I personally have mostly used good MVCC databases. For example, I have no DB2 on mainframe experience to call on. There might be people from a different set of experiences that can describe a real case for not having FK constraints enforced. I'd be happy to hear that story.

rbygrave avatar Apr 10 '16 23:04 rbygrave

I'm happy for you to still push for this feature ... but I want more substance to the argument. I'll close this for now but re-open if you want to add to this and still push for the feature.

Thanks, Rob.

rbygrave avatar Apr 12 '16 11:04 rbygrave

I am also of the oppinion that database constraints is not as good as well written repository/abstraction layer code. For the following reasons:

  1. By adding the constraints into the database, you give the idea that the database is responsible for consistance - which it can never be, only the business logic can. Example: consider a table "parent" with two columns: "gender" and "type". With the risk of provoking people of non-simple (Male/Female) gender one could argue that a row with "gender": "Male" and "type": "Mom" is inconsistant (There may be better examples, but I guess you get the point). Hence, consistancy is more than just keeping track of ids not pointing into nowhere, and only the application logic is capable of governing all aspects.
  2. DB-based constraints make code more complicated because they force linked objects to be created/deleted in a specific order - not just all put in the same transaction.
  3. They disallow "on delete set null" behaviour, where you (as an application developer) assumes that any reference which points into nowhere is actually null. This is nice because any optional reference does not have to be updated if the target is deleted, deleting it will effectively mean the reference is set to null when you read it the next time.

It is my experience that with a strong ORM like EBean (plus plumming like lombok), DBMS-based constraints is not really necessary. This is because all the autogenerated getters/setters, the one-to-many/many-to-ones makes writing application logic so easy that keeping track of consistancy in the application layer is not an unreasonable demand to an application programmer.

oak1980 avatar Jan 09 '17 08:01 oak1980

On Mon, Jan 9, 2017 at 3:00 AM, oak1980 [email protected] wrote:

I am also of the oppinion that database constraints is not as good as well written repository/abstraction layer code.

I say database constraints are always necessary. Well written repository/abstraction layer code is nice to have. The former guarantees data consistency, the latter usually allows more graceful handling of inconsistency.

The database IS responsible for consistency in the sense that it is the only way to guarantee it. Even if your app is all Ebean some developer may write a migration script of some kind that generates bad data so point #1 doesn't hold. Constraints can be designed to "defer" on the matter of order in transactions so your point #2 doesn't hold. I know my database allows "on delete set null" so point #3 doesn't hold, unless I misunderstand.

Programmers make mistakes, foreign key constraints, check constraints, etc. are a strong defense against mistakes.

I don't have a particularly strong feeling about adding this feature, I just know I would never use it and I think it would be bad practice to use it.

/Daryl

dastultz avatar Jan 09 '17 12:01 dastultz

Could you please share examples for 2 and 3 with Ebean (such that anyone looking for this feature see an easy workaround):

  1. How to OneToMany/ManyToOne link two classes and delete them in the "wrong" order

  2. How to OneToMany/ManyToOne link two classes and and then annotate the relationship to "SET NULL" when the "one" is deleted.

oak1980 avatar Jan 09 '17 13:01 oak1980

On Mon, Jan 9, 2017 at 8:46 AM, oak1980 [email protected] wrote:

Could you please share examples for 2 and 3 with Ebean (such that anyone looking for this feature see an easy workaround):

Sorry, no, I don't use Ebean to do these things. We manage the database schema with another tool.

/Daryl

dastultz avatar Jan 09 '17 14:01 dastultz