Replies: 1 comment
-
I believe this is relevant for an issue I've recently run into as well. This, however, creates problems down the line, like the following example: from typing import Optional
import ormar
import sqlalchemy
from app.core.db import database
class BaseMeta(ormar.ModelMeta):
database = database
metadata = sqlalchemy.MetaData()
class AppUser(ormar.Model):
class Meta(BaseMeta):
tablename = "app_users"
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=256)
class AppGroup(ormar.Model):
class Meta(BaseMeta):
tablename = "app_groups"
id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=256)
description: Optional[str] = ormar.String(max_length=256, nullable=True)
members: Optional[list[AppUser]] = ormar.ManyToMany(AppUser, related_name='member_users') If I create a user and a group, and add this user twice to the members-field of the import pytest
from app.core.db import database
from app.models.access import AppUser, AppGroup
@pytest.mark.asyncio
async def test_db():
async with database:
app_user = await AppUser.objects.create(name='User added twice to the same group')
app_group = await AppGroup.objects.create(name='Group with default through model')
await app_group.members.add(app_user)
await app_group.members.add(app_user)
await app_group.members.remove(app_user) I use """Add app-access
Revision ID: de9f6a3b6363
Revises: 3a81a18d3296
Create Date: 2022-11-07 09:22:47.615231
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'de9f6a3b6363'
down_revision = '3a81a18d3296'
branch_labels = None
depends_on = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('app_groups',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=256), nullable=False),
sa.Column('description', sa.String(length=256), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('app_users',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=256), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('appgroups_appusers',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('appuser', sa.Integer(), nullable=True),
sa.Column('appgroup', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['appgroup'], ['app_groups.id'], name='fk_appgroups_appusers_app_groups_appgroup_id', onupdate='CASCADE', ondelete='CASCADE'),
sa.ForeignKeyConstraint(['appuser'], ['app_users.id'], name='fk_appgroups_appusers_app_users_appuser_id', onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id'),
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('appgroups_appusers')
op.drop_table('app_users')
op.drop_table('app_groups')
# ### end Alembic commands ### In the migration file I can add a unique constraint to the autogenerated through-table |
Beta Was this translation helpful? Give feedback.
-
I am trying to use such code:
It fails because of
ormar.UniqueColumns
constraint I am trying to apply.If I remove it, the error disappears.
But it is a necessary requirement for this constraint to exist.
It is typical to many-to-many relation.
I tried many times with different variations, tried to use some hacks, but nothing helps me. It always throws either this error:
or similar ones.
I tried to pass constraints array to ormar.ManyToMany
I tried to use forwardref in different combinations
I tried to use original unique constraint from sqlalchemy
I tried to add these 2 columns from constraint to
Meta
ofTickerToTickerCollection
and few others
It does not work
Beta Was this translation helpful? Give feedback.
All reactions