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

Repeated columns of the same class fails #75

Open
kestral1 opened this issue Dec 12, 2023 · 0 comments
Open

Repeated columns of the same class fails #75

kestral1 opened this issue Dec 12, 2023 · 0 comments

Comments

@kestral1
Copy link

What does happen

from pydbantic import DataBaseModel
import asyncio
from pydbantic import Database

class ClassA(DataBaseModel):
    a_attribute: int

class ClassB(DataBaseModel):
    b_attribute: int
    b_foreign_key_1: ClassA = None
    b_foreign_key_2: ClassA = None


async def main():
    db = await Database.create(
        'sqlite:///test.db',
        tables=[
            ClassA,
            ClassB,
        ]
    )

    a_1 = await ClassA.create(a_attribute=1)
    a_2 = await ClassA.create(a_attribute=2)

    class_b = ClassB(
        b_attribute=1,
        b_foreign_key_1 = a_1,
        b_foreign_key_2 = a_2,
    )

    await class_b.save()

if __name__ == '__main__':
    asyncio.run(main())

This returns an error

sqlite3.OperationalError: ambiguous column name: ClassB_to_ClassA.ClassB_b_attribute

What should happen

A possible solution is: PyDBantic saves relationship tables with the column name as well. That way instead of a single ClassB_to_ClassA column, we generate ClassB_to_ClassA_b_foreign_key_1, and ClassB_to_ClassA_b_foreign_key_2.

@kestral1 kestral1 changed the title multiple instances of the same DataBaseModel Repeated columns of the same class fails Dec 12, 2023
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

No branches or pull requests

1 participant