Skip to content

fix(postgres): resolve enum column change syntax error #56523

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

Draft
wants to merge 3 commits into
base: 12.x
Choose a base branch
from

Conversation

almontasser
Copy link

PostgreSQL enum column changes were failing with syntax errors because Laravel was generating invalid SQL that included CHECK constraints in ALTER COLUMN TYPE statements.

Changes:

  • Modified PostgresGrammar::typeEnum() to return only varchar(255) for ALTER COLUMN contexts, avoiding inline CHECK constraints
  • Enhanced PostgresGrammar::compileChange() to handle enum columns with separate statements: DROP old constraint, ALTER column type, ADD new constraint
  • Added comprehensive tests for PostgreSQL enum column changes

Fixes the error: "syntax error at or near 'check'" when changing enum column definitions in PostgreSQL migrations.

Before:
ALTER COLUMN "status" TYPE varchar(255) check ("status" in (...)) -- INVALID

After:
DROP CONSTRAINT IF EXISTS table_column_check;
ALTER COLUMN "status" TYPE varchar(255);
ADD CONSTRAINT table_column_check CHECK ("status" in (...)); -- VALID

PostgreSQL enum column changes were failing with syntax errors because
Laravel was generating invalid SQL that included CHECK constraints in
ALTER COLUMN TYPE statements.

Changes:
- Modified PostgresGrammar::typeEnum() to return only varchar(255) for
  ALTER COLUMN contexts, avoiding inline CHECK constraints
- Enhanced PostgresGrammar::compileChange() to handle enum columns with
  separate statements: DROP old constraint, ALTER column type, ADD new constraint
- Added comprehensive tests for PostgreSQL enum column changes

Fixes the error: "syntax error at or near 'check'" when changing enum
column definitions in PostgreSQL migrations.

Before:
ALTER COLUMN "status" TYPE varchar(255) check ("status" in (...))  -- INVALID

After:
DROP CONSTRAINT IF EXISTS table_column_check;
ALTER COLUMN "status" TYPE varchar(255);
ADD CONSTRAINT table_column_check CHECK ("status" in (...));  -- VALID
PostgreSQL may return different column type names (e.g., 'varchar' vs
'character varying', 'int4' vs 'integer') depending on configuration.

Updated assertions to use assertContains with arrays of valid type names
to ensure tests pass consistently across different PostgreSQL setups.
@taylorotwell
Copy link
Member

@hafezdivandari what do you think about this one?

@taylorotwell taylorotwell marked this pull request as draft August 3, 2025 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants