Repeated columns of the same class fails
Opened this issue · 0 comments
kestral1 commented
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
.