{"id":"4808b48ab6281f33","repo":"sqlalchemy/alembic","slug":"type-can-be-one-of-s","errorCode":null,"errorMessage":"'type' can be one of %s","messagePattern":"'type' can be one of (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"alembic/operations/schemaobj.py","lineNumber":179,"sourceCode":"        table_name: str,\n        type_: str | None,\n        schema: str | None = None,\n        **kw,\n    ) -> Any:\n        t = self.table(table_name, schema=schema)\n        types: dict[str | None, Any] = {\n            \"foreignkey\": lambda name: sa_schema.ForeignKeyConstraint(\n                [], [], name=name\n            ),\n            \"primary\": sa_schema.PrimaryKeyConstraint,\n            \"unique\": sa_schema.UniqueConstraint,\n            \"check\": lambda name: sa_schema.CheckConstraint(\"\", name=name),\n            None: sa_schema.Constraint,\n        }\n        try:\n            const = types[type_]\n        except KeyError as ke:\n            raise TypeError(\n                \"'type' can be one of %s\"\n                % \", \".join(sorted(repr(x) for x in types))\n            ) from ke\n        else:\n            const = const(name=name)\n            t.append_constraint(const)\n            return const\n\n    def metadata(self) -> MetaData:\n        kw = {}\n        if (\n            self.migration_context is not None\n            and \"target_metadata\" in self.migration_context.opts\n        ):\n            mt = self.migration_context.opts[\"target_metadata\"]\n            if hasattr(mt, \"naming_convention\"):\n                kw[\"naming_convention\"] = mt.naming_convention\n        return sa_schema.MetaData(**kw)","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/sqlalchemy/alembic/blob/44fb3450330204b222ff05135e1fbbbdb28c44db/alembic/operations/schemaobj.py#L161-L197","documentation":"Raised by SchemaObjects.generic_constraint() when the type_ argument is not one of the allowed keys ('foreignkey','primary','unique','check',None). The method maps type_ to a SQLAlchemy constraint constructor via a dict; an unknown type raises TypeError listing the valid values. generic_constraint is the internal factory used to render drop_constraint and similar ops.","triggerScenarios":"Calling schema_obj.generic_constraint(name, table, type_='bogus') with an invalid type; a DropConstraintOp whose constraint_type is not normalized to one of the allowed values feeding into generic_constraint; typos like 'foreign' instead of 'foreignkey', 'uniq' instead of 'unique', 'pk' instead of 'primary'.","commonSituations":"Custom code that constructs constraint type strings manually; an op object built with an unsupported type string; version skew where a new constraint type isn't registered.","solutions":["Pass one of the valid type_ values: 'foreignkey', 'primary', 'unique', 'check', or None.","Build the op via the high-level API (op.drop_constraint(..., type_='foreignkey')) which documents the accepted strings.","Validate the type_ string against the allowed set before calling generic_constraint()."],"exampleFix":"// before\nschema_obj.generic_constraint('c', 'user', type_='foreign')  # raises TypeError\n\n// after\nschema_obj.generic_constraint('c', 'user', type_='foreignkey')","handlingStrategy":"validation","validationCode":"VALID_CONSTRAINT_TYPES = {'foreignkey', 'primary', 'unique', 'check', None}\n\ndef validate_constraint_type(type_):\n    if type_ not in VALID_CONSTRAINT_TYPES:\n        raise ValueError(f'invalid constraint type {type_!r}; expected one of {VALID_CONSTRAINT_TYPES}')\n    return type_","typeGuard":"def is_valid_constraint_type(type_) -> bool:\n    return type_ in {'foreignkey', 'primary', 'unique', 'check', None}","tryCatchPattern":"try:\n    schema_obj.generic_constraint(name, table, type_=t)\nexcept TypeError as e:\n    if \"'type' can be one of\" in str(e):\n        # map/correct the type string\n        schema_obj.generic_constraint(name, table, type_=normalized_t)\n    else:\n        raise","preventionTips":["Use the documented type_ strings: 'foreignkey','primary','unique','check'.","Validate type_ against the allowed set before calling generic_constraint.","Build ops via the high-level API to get correct type normalization."],"tags":["alembic","constraints","migrations","internals","validation"],"analyzedSha":"44fb3450330204b222ff05135e1fbbbdb28c44db","analyzedAt":"2026-08-04T19:57:10.248Z","schemaVersion":2}