Schema Tool: Ignore some database fields #5531

Closed
opened 2026-01-22 15:10:16 +01:00 by admin · 5 comments
Owner

Originally created by @mpdude on GitHub (May 10, 2017).

When working with legacy databases, I often have some columns in the database that have no corresponding field in the entity class (for various reasons probably not relevant here).

This makes the schema tool almost useless as it always tries to drop or otherwise change these columns. You'll always have to carefully cherry-pick the "real" changes.

I know that the schema tool can be configured to ignore certain tables, but that does (to my knowledge) not work on the field level.

So, my idea was to add entity configuration meta-data to denote these "ignored" fields. But as I am writing this, adding something like the FilterSchemaAssetsExpression in \Doctrine\DBAL\Configuration for the field level seems like another possible solution (yes, that's another repo).

What do lead devs think? Would you support such a PR?

Originally created by @mpdude on GitHub (May 10, 2017). When working with legacy databases, I often have some columns in the database that have no corresponding field in the entity class (for various reasons probably not relevant here). This makes the schema tool almost useless as it always tries to drop or otherwise change these columns. You'll always have to carefully cherry-pick the "real" changes. I know that the schema tool can be configured to ignore certain tables, but that does (to my knowledge) not work on the field level. So, my idea was to add entity configuration meta-data to denote these "ignored" fields. But as I am writing this, adding something like the `FilterSchemaAssetsExpression` in `\Doctrine\DBAL\Configuration` for the field level seems like another possible solution (yes, that's another repo). What do lead devs think? Would you support such a PR?
admin closed this issue 2026-01-22 15:10:17 +01:00
Author
Owner

@Ocramius commented on GitHub (May 10, 2017):

@mpdude I had this scenario multiple times, but this drags in an additional family of problems, as this would then leak also to FKs, constraints, indexes, sequences.

Not sure we want to dig into this rabbit hole, but you can try. What would you imagine the filters to look like?

@Ocramius commented on GitHub (May 10, 2017): @mpdude I had this scenario multiple times, but this drags in an additional family of problems, as this would then leak also to FKs, constraints, indexes, sequences. Not sure we want to dig into this rabbit hole, but you can try. What would you imagine the filters to look like?
Author
Owner

@mpdude commented on GitHub (May 10, 2017):

Oh, good point!

If it were a config setting in the DBAL layer, simply pretending that the fields would not be there – are FKs, indexes etc. an issue at all?

Sure, if you get this config wrong by hiding a field that is actually needed, you might be surprised why the schema tool tries to add it. But, either way, chances are somebody gets hurt.

As to how filters could look like, I probably don't get the point... Maybe a regex? :-)

@mpdude commented on GitHub (May 10, 2017): Oh, good point! If it were a config setting in the DBAL layer, simply pretending that the fields would not be there – are FKs, indexes etc. an issue at all? Sure, if you get this config wrong by hiding a field that is actually needed, you might be surprised why the schema tool tries to add it. But, either way, chances are somebody gets hurt. As to how filters could look like, I probably don't get the point... Maybe a regex? :-)
Author
Owner

@Ocramius commented on GitHub (May 10, 2017):

Maybe a regex? :-)

Callback?

@Ocramius commented on GitHub (May 10, 2017): > Maybe a regex? :-) Callback?
Author
Owner

@dbu commented on GitHub (Feb 26, 2019):

Note that it is possible to make the Doctrine Schema tool not see database fields with the onSchemaColumnDefinition event, and also for indexes with onSchemaIndexDefinition.

I wrote a blogpost with code examples: https://www.liip.ch/en/blog/doctrine-and-generated-columns

The listener is rather heavy though, so an explicit configuration would be easier to understand and involve less effort.

@dbu commented on GitHub (Feb 26, 2019): Note that it is possible to make the Doctrine Schema tool not see database fields with the onSchemaColumnDefinition event, and also for indexes with onSchemaIndexDefinition. I wrote a blogpost with code examples: https://www.liip.ch/en/blog/doctrine-and-generated-columns The listener is rather heavy though, so an explicit configuration would be easier to understand and involve less effort.
Author
Owner

@Brewal commented on GitHub (Nov 20, 2023):

What I did which works for me, is to use the postGenerateSchema event to manually add the columns, indexes and foreign keys of the fields you want to ignore to the schema. Here is the gist :

https://gist.github.com/Brewal/4a623208e7cd60c4dfb5ab9ab56bcb2e

@Brewal commented on GitHub (Nov 20, 2023): What I did which works for me, is to use the `postGenerateSchema` event to manually add the columns, indexes and foreign keys of the fields you want to ignore to the schema. Here is the gist : https://gist.github.com/Brewal/4a623208e7cd60c4dfb5ab9ab56bcb2e
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5531