0

I have created two tables a and b which are related to each other in a many to many relationship using a table a_to_b. The code below is how I have implemented it. In the table a_to_b I need an id column which is an integer but not auto incremented. I need this to identify the relationship for another use case.

a_to_b = Table(
    "a_to_b",
    ormbase.metadata,
    Column("id", Integer(), nullable=False),
    Column("a_id", Integer(), ForeignKey("a.id"), primary_key=True),
    Column("b_id", Integer(), ForeignKey("b.id"), primary_key=True),
)


class A(ormbase):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True, autoincrement=True)
    column1 = Column(String)
    ...
    
    b_rel = relationship("b", secondary=a_to_b, backref="a_rel")

class B(ormbase):
    __tablename__ = 'b'
    id = Column(Integer, primary_key=True, autoincrement=True)
    column1 = Column(String)
    ...

This all works fine until I try to add a relationship between two objects. To add a relationship between two objects generally I could just append the related object to the relationship attribute like this:

a = A(column1="some info"...)
b = B(column1="some other info"...)

a.b_rel.append(b)
session.add(a)
session.flush()

But when I have an extra id column I get the following error:

(psycopg2.errors.NotNullViolation) null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, 1, 183).

Now I know I have to specify the id value since it is not auto incrementing. I do not know how to do it. Any help or examples or reference links would be appreciated.

leshravnya
  • 93
  • 10

0 Answers0