Hello,

I asked this question on Reddit and StackOverflow without any luck getting 
an answer. Here is the StackOverflow question 
<https://stackoverflow.com/questions/45599453/set-up-a-composite-key-using-a-foreign-key-and-another-column-with-python-sqlalc>.
 
I'll paste it below again.


I have two tables set up in Python with sqlalchemy using mySQL. They look 
something like this:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

class Test(Base):
    __tablename__ = 'test'
    id = Column(Integer, primary_key=True)
    questions = relationship('Question', cascade='all,delete', backref=
'test', uselist=True)

class Question(Base):
    __tablename__ = 'question'
    testID = Column(Integer, ForeignKey('test.id', ondelete='CASCADE'))
    sequence = Column(Integer, primary_key=True)
    text = Column(String(500))



Right now when I run my code and get the tables set up, they look like this:

testID|sequence|text

1 | 1 | text

1 | 2 | text

1 | 3 | text

1 | 4 | text

2 | 5 | text

2 | 6 | text

2 | 7 | text

But I want them to look like this:

testID|sequence|text

1 | 1 | text

1 | 2 | text

1 | 3 | text

1 | 4 | text

2 | 1 | text

2 | 2 | text

2 | 3 | text

I know that sequence is auto-incrementing because it is the first int 
primary key, but I only want it to auto-increment until there's a new 
testID.

I *could* leave it and just sort them later, but I'd really like to set it 
up where sequence resets with every new testID. I'm pretty new to 
sqlAlchemy so I may be missing something obvious. Any help is appreciated. 
Thanks.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to