-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Remove mentions of Eloquent truncate from docs #10116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
If the method exists, it should be documented 🤷🏻 Wouldn't it make more sense to create a PR to remove it from https://github.com/laravel/framework first? Instead, you could add a warning to the docs. |
|
@shaedrich , if you look through linked issues, you can see, that method cannot be simply removed. It comes with framework over tenth years. Even little changing it's behaviour only for postgresql to less distructive cannot be done, as it would be breaking change. |
Withholding information from the actual state of the framework is, in my opinion, similar to lecturing people but with the downside that it patronizes people.
You can, however, mark methods as |
Ok, can we write something like "You should think twice before using truncate method. You probably do not need it and should use delete" ? |
As said in #10118, as long, as we explicitly write that into the docs instead of just removing it, I'd be fine with that. But in the end, it's not for me to finally decide that. |
|
This could just be sent to 11.x branch for consideration. |
There are some issues and long dicsussions around
truncatemethod and PostgreSQL CASCADE behaviour.some of them (and many more pull requests):
useCascadeTruncatemethod forPostgresGrammarframework#53343Currently documentation says
This means that all foreign key related records in other tables will be deleted as well.. This wording is very soft and does not highlight the full danger. Truncate cascade will fully truncate any tables, that has foreign keys pointing current table. Not only those records, that has foreign keys. Records, having null in foreign key columns will be also deleted.While discussing if postgresql should use CASCADE in truncates, no one speaks, that using truncate is bad practice at all.
First of all, TRUNCATE SQL command is part of Data Definition Language (DDL), and DELETE is part of Data Manipulation Language (DML). So, using TRUNCATE is ok somewhere in migrations, but not in regular code. In other words, delete is for developers, truncate is for database administrators. More at https://stackoverflow.com/a/139633
Depending on sql server truncate may not respect transactions and many checks in database integrity. Delete always respects all currently enabled data integrity mechanisms (foreign keys, constraints, etc).
So, using Truncate in regular code (meaning code, that manipulates data) is bad pattern. Mentioning Truncate in the same context as Delete in the documentation gives new developers the incorrect impression that these commands are equivalent operations. We cannot change the behavior of the framework without breaking backward compatibility. But we can remove mention of truncate from the documentation, and we will save many novice developers from stupid mistakes. Experienced developers will easily find the truncate command in the source code and can use it at their own risk.
Following the rule that all undocumented methods are used at your own risk, removing the truncate method from the documentation will remove all further questions about how exactly this method should work, reducing the number of complaints to the framework developers about the fact that the method does not work as expected. This reduce need of support recently introduced PostgresGrammar::$cascadeTruncate and change it to false in future.
Introduce this change in documentation (and idea, that regular developers and new projects should not use truncate) together with release of Laravel 12 is the most gentle way to solve this complicated situation