Skip to content

Migrations for FK fields #34

@jmriebold

Description

@jmriebold

I've run into a couple issues with the migrations generated for foreign key fields, specifically that neither db_index nor db_unique are respected for FKs.

For example, the following models (adapted from the docs)

from oxyde import Field, Model


class Author(Model):
    id: int | None = Field(default=None, db_pk=True)
    name: str

    class Meta:
        is_table = True


class Post(Model):
    id: int | None = Field(default=None, db_pk=True)
    title: str
    author: Author = Field(default=None, db_on_delete="CASCADE", db_index=True, db_nullable=False, db_unique=True)

    class Meta:
        is_table = True

result in this migration for Post:

    ctx.create_table(
        "post",
        fields=[
            {
                'name': 'id',
                'python_type': 'int',
                'db_type': None,
                'nullable': True,
                'primary_key': True,
                'unique': False,
                'default': None,
                'auto_increment': False,
                'max_length': None,
                'max_digits': None,
                'decimal_places': None
            },
            {
                'name': 'title',
                'python_type': 'str',
                'db_type': None,
                'nullable': False,
                'primary_key': False,
                'unique': False,
                'default': None,
                'auto_increment': False,
                'max_length': None,
                'max_digits': None,
                'decimal_places': None
            },
            {
                'name': 'author_id',
                'python_type': 'int',
                'db_type': None,
                'nullable': False,
                'primary_key': False,
                'unique': False,
                'default': None,
                'auto_increment': False,
                'max_length': None,
                'max_digits': None,
                'decimal_places': None
            }
        ],
        foreign_keys=[
            {
                'name': 'fk_post_author_id',
                'columns': [
                    'author_id'
                ],
                'ref_table': 'author',
                'ref_columns': [
                    'id'
                ],
                'on_delete': 'CASCADE',
                'on_update': 'CASCADE'
            }
        ],
    )

Post.author should have an index and a unique constraint, but has neither.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions