Note that there are some explanatory texts on larger screens.

plurals
  1. POsqlalchemy does not enforce foreign key constraing
    primarykey
    data
    text
    <p>I am trying to play with the sql alchemy ORM based db definition. I have defined my tables as follows</p> <pre><code>class Customer(Base): __tablename__ = 'customer' id = Column(Integer, primary_key=True) name = Column(String(80)) auth = relationship("CustomerAuth", backref='customer') class CustomerAuth(Base): __tablename__ = 'authentication' id = Column(Integer, ForeignKey('customer.id')) username = Column(String(80), primary_key=True) passwd = Column(String(80)) </code></pre> <p>Now I am creating the session</p> <pre><code>Session = sessionmaker(bind=sqla.engine) session = Session() </code></pre> <p>And then I try to create two row objects for Customer with id 1 and 2</p> <pre><code>cst1 = sqla.Customer(id=1,name='shyam') cst2 = sqla.Customer(id=2,name='ram') </code></pre> <p>And I create three row objects for CustomerAuth which reference to id 1, 2 and 3 of Customer</p> <pre><code>auth1 = sqla.CustomerAuth(id=1,username='shyamu',passwd='wam') auth2 = sqla.CustomerAuth(id=2,username='ramu',passwd='dam') auth3 = sqla.CustomerAuth(id=3,username='lamu',passwd='sam') </code></pre> <p>As you can see I have created a CustomerAuth row with id = 3 , which is a foreign key referring the Customer.id. But since Customer table has no entry with id=3 , this should fail</p> <pre><code>session.add(cst1) session.add(cst2) session.add(auth1) session.add(auth2) session.flush() </code></pre> <p>This operation should fail but it goes through successfully.</p> <pre><code>session.add(auth3) session.flush() </code></pre> <p>I want to know what am I not doing which bypasses the foreign key enforcement</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload