mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Custom Index definition with postgresql #5723
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @Vincz on GitHub (Oct 2, 2017).
Originally assigned to: @Ocramius on GitHub.
Hi! I was wondering how it would be possible to create GIN / GIST indexes with doctrine and the postgresql adapter.
Should we create our own type, like this:
https://github.com/jsor/doctrine-postgis/blob/master/src/Schema/SpatialIndexSqlGenerator.php
Or is there a built-in way in doctrine. Can't find any documentation on this.
Just trying to use the indexes from here : https://www.postgresql.org/docs/9.1/static/textsearch-indexes.html
Thx !
@Ocramius commented on GitHub (Oct 3, 2017):
No, the ORM has no way to map these index types at the moment.
In my opinion, this kind of information should only be mapped in SQL migrations, as it starts becoming extremely DB-vendor-specific.
@lcobucci commented on GitHub (Nov 26, 2017):
Closing as per @Ocramius' comment.
@Grafikart commented on GitHub (Aug 16, 2020):
Sorry to reopen such an old issue but I get the same problem. I add my index in migrations as you suggested but when I try to generate other migrations doctrine try to delete the index.
Is there a way to tell doctrine to ignore some custom indexes (inserted without annotation but with migrations) ?
@Grafikart commented on GitHub (Aug 16, 2020):
If someone find this issue I found a post about this problem. It may be a good solution : https://www.liip.ch/en/blog/doctrine-and-generated-columns
@alexsegura commented on GitHub (Sep 29, 2020):
You can do it like this, using index flags & a custom platform class.
If you are using Symfony, you can override the platform class using the
platform_serviceconfiguration key.@SherinBloemendaal commented on GitHub (Sep 12, 2022):
The method
getIndexFieldDeclarationListSQLis deprecated since https://github.com/doctrine/dbal/pull/5527. How should we handle this case in 2022?@janklan commented on GitHub (Nov 20, 2022):
I'm not sure why they used
@deprecatedand not@internal, but the code inAbstractPlatform::getIndexFieldDeclarationListSQL()points at https://github.com/doctrine/dbal/pull/5527, which says the methods will be made protected, not removed, so I think @alexsegura's code is still valid.@DaanBiesterbos commented on GitHub (Mar 21, 2024):
Regardless of which annotation is used, of what we assume. Fact is that it is deprecated. The "platform_service" configuration is now also deprecated in the doctrine-bundle for Symfony.
I need 1 thing. To add a GIS index to a migration file, and stop doctrine from interfering with it. And it looks like all I have are deprecated options and assumptions. I cannot deliver this to a client. I went over a number of issues. And I've only seen proposals being rejected. And I have read some excellent points which are all true about how the ORM should work. All of which are true.
However, in some cases, database specific features are needed. And I feel like doctrine is cutting off as many non standard use cases as possible. For the sake of simplicity, or performance maybe. But I feel like this is a mistake.
Either provide a solution (like a custom index) to allow us to use database specific features. (Without having doctrine try to remove all our indexes every single migration.......) Of provide a way to extend doctrine. Let us extend the Platform class. Don't deprecate it unless there is an alternative.
If I am wrong, I would love to hear it. As it would solve my problem.
But I just want a GIS index, and use tsvector functions in postgres (which I can add via the custom functions.
Why is Doctrine making it so difficult? Sure it should not be in the ORM, 100% true, but provide a way way to customize the index definition.
How is it possible that we can hook into the SQL parser, add custom functions, add custom column definitions. But customizing a simple index is a bridge too far.
As far as I am concerned adding an index should just be be possible. There is clearly a use case for it. Or is doctrine also planning to remove custom column definitions and support for custom functions? I certainly hope not...
I just need an index. And I need Doctrine to stop trying to remove the index(es) every time I create a migration. This is bad, annoying and could lead to serious problems when overlooked.
I hope you'll are willing to go over the I don't know how many related tickets and come to the same conclusion. Awesome work over the years guys, really. But please, fix this. We need this.
@DaanBiesterbos commented on GitHub (Mar 22, 2024):
This solution is also deprecated:
https://www.liip.ch/en/blog/doctrine-and-generated-columns
What is doctrine providing as an alternative?
@greg0ire commented on GitHub (Mar 22, 2024):
It feels to me like you did not find this one: https://github.com/doctrine/migrations/issues/831
@DaanBiesterbos commented on GitHub (Mar 25, 2024):
I found a similar example. I liked it... But these listeners are all deprecated in Symfony 6....
@DaanBiesterbos commented on GitHub (Mar 25, 2024):
I did, but the issue is open. And I noticed a related PR that was rejected.
There is a comment that links to this solution:
https://medium.com/yousign-engineering-product/ignore-custom-indexes-on-doctrine-dbal-b5131dd22071
These methods are also deprecated. So is the platform_service setting in the doctrine-bundle.
@greg0ire commented on GitHub (Mar 25, 2024):
Yes, that is what I was referring to.
What methods?
Looks like you are not forced to override the platform service. overriding the schema manager factory might be enough.
@DaanBiesterbos commented on GitHub (Mar 25, 2024):
Thank you for your response. I tried it. Unfortunately, when I do this the customizations from doctrine.yaml are not loaded (types etc). And the configured doctrine platform is not a service. So I cannot inject it.
When I override the schema manager factory, I get this exception:
Unknown database type bit requested, Doctrine\DBAL\Platforms\PostgreSQLPlatform may not support it.
When I don't extend the platform there is no service for the platform. So I guess it makes sense. Whether I instantiate the platform myself or inject a new instance, it seems to be separate from Symfony without the platform_service setting.
Maybe I missed something somewhere, but since the original platform is not available as a service I decided not to waste too much time on it.
When I use the platform service is does seem to work (although I did not test everything).
Sadly, the platform_service setting is also deprecated.
I tried the subscriber as well.
I can automatically generate the index this way. Which would be nice, were it not that all these events are deprecated. And don't work exactly as I would expect. Well, for the column, I do understand that the index definition event might depend on the index attributes, which are not available for custom indexes.
So far, I cannot keep doctrine from 1. trying to delete the index and 2. alter the tsvector column definition every time I generate a new migration.
I verified that the preventDefault method is invoked for the altered column, but it would seem the alter table statements are rendered regardless.
This would be perfect.......... If it worked....
The first migration looks like this:
The following four statements are generated every single migration.
TsVectorType
Product entity:
@DaanBiesterbos commented on GitHub (Mar 26, 2024):
Edit:
I was able to fix the subscriber. That works fine. Although the events are still deprecated. And it does not yet avoid the deletion of the index.
@DaanBiesterbos commented on GitHub (Mar 26, 2024):
This was the best solution I could come up with. I still need the platform_service. But I don't need the subscriber anymore. The solution should be okayish. Although that's not really what I like to be aiming for.
I extended the PostgreSQLPlatform class:
Extend PostgreSQLSchemaManager:
Configuration in packages/doctrine.yaml:
@boedy commented on GitHub (Oct 25, 2024):
The proposed solution seems unnecessarily complex for such a straightforward requirement. All that’s being asked is to define the index type, which is essentially just a string.
Sure, different databases have unique index types, but requiring complex workarounds for this seems excessive. Why not keep it simple for specifying these index types, so users can just set them directly without extra configuration? 🤷♂️