Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:14:58+00:00 2026-06-04T01:14:58+00:00

I have a set of tables that look like: workflows = Table(‘workflows’, Base.metadata, Column(‘id’,

  • 0

I have a set of tables that look like:

workflows = Table('workflows', Base.metadata,
                  Column('id', Integer, primary_key=True),
                 )

actions = Table('actions', Base.metadata,
                Column('name', String, primary_key=True),
                Column('workflow_id', Integer, ForeignKey(workflows.c.id), primary_key=True),
               )

action_dependencies = Table('action_dependencies', Base.metadata,
                            Column('workflow_id', Integer, ForeignKey(workflows.c.id), primary_key=True),
                            Column('parent_action', String, ForeignKey(actions.c.name), primary_key=True),
                            Column('child_action', String, ForeignKey(actions.c.name), primary_key=True),
                           )

My ORM classes look like:

class Workflow(Base):
    __table__ = workflows

    actions = relationship("Action", order_by="Action.name", backref="workflow")


class Action(Base):
    __table__ = actions

    children = relationship("Action",
                            secondary=action_dependencies,
                            primaryjoin=actions.c.name == action_dependencies.c.parent_action,
                            secondaryjoin=actions.c.name == action_dependencies.c.child_action,
                            backref="parents"
                           )

So in my system, each action is uniquely identified by a combination of a workflow id and its name. I’d like each action to have parents and children attribute that refers its parent and child actions. Each action can have multiple parents and children.

The problem occurs when I have a function such as :

def set_parents(session, workflow_id, action_name, parents):
    action = session.query(db.Action).filter(db.Action.workflow_id == workflow.id).filter(db.Action.name == action_name).one()

    for parent_name in parents:
        parent = session.query(db.Action).filter(db.Action.workflow_id == workflow.id).filter(db.Action.name == parent_name).one()
        action.parents.append(parent)

    session.commit()

I get an error like:

IntegrityError: (IntegrityError) action_dependencies.workflow_id may not be NULL u'INSERT INTO action_dependencies (parent_action, child_action) VALUES (?, ?)' (u'directory_creator', u'packing')

How do I get the relationship to set the workflow_id correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:15:00+00:00Added an answer on June 4, 2026 at 1:15 am

    See below working code. The key points are those I mentioned in the comments:

    • proper composite ForeignKeys
    • correct relationship configuration using the FKs

    Code:

    workflows = Table('workflows', Base.metadata,
                      Column('id', Integer, primary_key=True),
                     )
    
    actions = Table('actions', Base.metadata,
                    Column('workflow_id', Integer, ForeignKey(workflows.c.id), primary_key=True),
                    Column('name', String, primary_key=True),
                   )
    
    action_dependencies = Table('action_dependencies', Base.metadata,
                                Column('workflow_id', Integer, ForeignKey(workflows.c.id), primary_key=True),
                                Column('parent_action', String, ForeignKey(actions.c.name), primary_key=True),
                                Column('child_action', String, ForeignKey(actions.c.name), primary_key=True),
                                ForeignKeyConstraint(['workflow_id', 'parent_action'], ['actions.workflow_id', 'actions.name']),
                                ForeignKeyConstraint(['workflow_id', 'child_action'], ['actions.workflow_id', 'actions.name']),
                               )
    class Workflow(Base):
        __table__ = workflows
        actions = relationship("Action", order_by="Action.name", backref="workflow")
    
    class Action(Base):
        __table__ = actions
        children = relationship("Action",
                                secondary=action_dependencies,
                                primaryjoin=and_(actions.c.name == action_dependencies.c.parent_action,
                                    actions.c.workflow_id == action_dependencies.c.workflow_id),
                                secondaryjoin=and_(actions.c.name == action_dependencies.c.child_action,
                                    actions.c.workflow_id == action_dependencies.c.workflow_id),
                                backref="parents"
                               )
    
    # create db schema
    Base.metadata.create_all(engine)
    
    # create entities
    w_1 = Workflow()
    w_2 = Workflow()
    a_11 = Action(name="ac-11", workflow=w_1)
    a_12 = Action(name="ac-12", workflow=w_1)
    a_21 = Action(name="ac-21", workflow=w_2)
    a_22 = Action(name="ac-22", workflow=w_2)
    session.add(w_1)
    session.add(w_2)
    a_22.parents.append(a_21)
    session.commit()
    session.expunge_all()
    print '-'*80
    
    # helper functions
    def get_workflow(id):
        return session.query(Workflow).get(id)
    def get_action(name):
        return session.query(Action).filter_by(name=name).one()
    
    # test another OK
    a_11 = get_action("ac-11")
    a_12 = get_action("ac-12")
    a_11.children.append(a_12)
    session.commit()
    session.expunge_all()
    print '-'*80
    
    # test KO (THIS SHOULD FAIL VIOLATING FK-constraint)
    a_11 = get_action("ac-11")
    a_22 = get_action("ac-22")
    a_11.children.append(a_22)
    session.commit()
    session.expunge_all()
    print '-'*80
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables in my database that look like this CREATE TABLE IF
I have 3 tables that look like this: Table 1 (main table): item_id (item_id,
I have a two tables that look like this (this is an example of
I have to work with multiple SQL Server tables that generally look like this:
I have a set of tables which basically look like this AGENCY - AGENCY_NAME
I have three tables that look like these: PROD Prod_ID|Desc ------------ P1|Foo1 P2|Foo2 P3|Foo3
I have a couple of very large tables (over 400,000 rows) that look like
We have a set of tables and views that merely store some config data
I have an SQL database with a set of tables that have Unique Id's.
If you have a set of tables in the database that strictly consist of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.