Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Alchemy default value function for simulating autoincrement within a unique group of parent-child records
    primarykey
    data
    text
    <p>I have a small problem that I think should be easily handled by SQL Alchemy but I can't seem to get it right. I have two tables with one being a parent table and the other a child table. For each child record it needs a unique ID but only with the context of the unique parent record.</p> <p>I am using the Declarative Base approach.</p> <p>I setup the parent child relationship using the FKs and the relation function. What I'm trying to achieve is to get something like a pseudo autoincrement function that will lookup the max CategoryID value within the Category unique name group and increment it up one. I've tried using various default functions but the problem I run into is the inability to specify the CategoryUniqueName at the time of insertion. I can't find a way to pass the current value of CategoryItems.CategoryUniqueName so that the lookup query has the correct filter applied when trying to select something like func.max(CategoryItems.CategoryID). If I hardcode a query it works just fine. This is what I'm thinking should work but, again, I can't find a way to specify the unique value for the filter.</p> <pre><code>unique_group='my_group' result=con.query(func.max(CategoryItems.CategoryID)).filter(and_( CategoryItems.CategoryUniqueName==unique_group, )).one() </code></pre> <p>The classes are shown below. Much appreciate some guidance on how to accomplish this inside standard SQL Alchemy. I know I could always lookup the value and simply specify it directly within the same transaction but I'm trying to come up with a stand-alone SQL Alchemy approach that does not require additional logic elsewhere.</p> <pre><code>class Category(Base): __tablename__ = 'parent_table' __table_args__ = {'mysql_engine':'InnoDB', 'useexisting':True} CategoryUniqueName = Column(Unicode(255), primary_key=True) CategoryGroupName = Column(Unicode(255), nullable=False) CategoryGroupMemo = Column(UnicodeText) SortOrder = Column(Integer, index=True) IsLocked = Column(Boolean, default=0) class CategoryItems(Base): __tablename__ = 'child_table' __table_args__ = {'mysql_engine':'InnoDB', 'useexisting':True} CategoryUniqueName = Column(Unicode(255), ForeignKey(Category.CategoryUniqueName), primary_key=True) CategoryID = Column(Integer, primary_key=True, autoincrement=False) CategoryName = Column(Unicode(255), nullable=False, index=True) CategoryMemo = Column(UnicodeText) CategoryImage = Column(Unicode(255)) CategoryFlex1 = Column(Unicode(255), index=True) CategoryFlex2 = Column(Unicode(255), index=True) CategoryFlex3 = Column(Unicode(255), index=True) SortOrder = Column(Integer, index=True) category_group = relation( Category, backref=backref( 'items', order_by=SortOrder, collection_class=ordering_list('SortOrder'), cascade="all, delete, delete-orphan" )) </code></pre>
    singulars
    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