Skip to content
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

Model inheritance with primary_key not working #253

Closed
derzinn opened this issue Jun 24, 2021 · 3 comments
Closed

Model inheritance with primary_key not working #253

derzinn opened this issue Jun 24, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@derzinn
Copy link

derzinn commented Jun 24, 2021

Playing around with Ormar, I found that model inheritance wasn't working as expected.

class BaseMeta(ormar.ModelMeta):
    database = database
    metadata = metadata


class BaseModel(ormar.Model):
    class Meta:
        abstract = True

    id: uuid.UUID = ormar.UUID(
        primary_key=True,
        default=uuid.uuid4,
        uuid_format="string"
    )

    created_at: datetime.datetime = ormar.DateTime(default=datetime.datetime.utcnow())
    updated_at: datetime.datetime = ormar.DateTime(default=datetime.datetime.utcnow())


class Member(BaseModel):
    class Meta(BaseMeta):
        tablename = "members"

    first_name: str = ormar.String(max_length=50)
    last_name: str = ormar.String(max_length=50)

If i run await Member.objects.create(first_name="foo", last_name="bar") i got the following error:

asyncpg.exceptions.NotNullViolationError: null value in column "id" of relation "members" violates not-null constraint
DETAIL: Failing row contains (null, 2021-06-24 18:15:38.164825, 2021-06-24 18:15:38.164956, foo, bar).

How do I get this example working?


ormar 0.10.12
databases 0.4.3
psycopg2 2.9.1

@derzinn derzinn added the bug Something isn't working label Jun 24, 2021
@derzinn
Copy link
Author

derzinn commented Jun 24, 2021

I don't know if that helps, but Alembic is properly generating the tables from the inherited model example.

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('members',
    sa.Column('id', sa.CHAR(36), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('first_name', sa.String(length=50), nullable=False),
    sa.Column('last_name', sa.String(length=50), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

@collerek
Copy link
Owner

Fixed in 0.10.13 (via #254), please update and check again.
Thanks for reporting!

@derzinn
Copy link
Author

derzinn commented Jun 25, 2021

It works now as expected, thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants