简体   繁体   English

使用外键和Python / sqlAlchemy的另一列设置复合键

[英]Set up a composite key using a foreign key and another column with Python/sqlAlchemy

I have two tables set up in Python with sqlalchemy using mySQL. 我在使用mySQL的sqlalchemy中用Python设置了两个表。 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 testID |序列|文

1 | 1 | 1 | 1 | text 文本

1 | 1 | 2 | 2 | text 文本

1 | 1 | 3 | 3 | text 文本

1 | 1 | 4 | 4 | text 文本

2 | 2 | 5 | 5 | text 文本

2 | 2 | 6 | 6 | text 文本

2 | 2 | 7 | 7 | text 文本

But I want them to look like this: 但我希望它们看起来像这样:

testID|sequence|text testID |序列|文

1 | 1 | 1 | 1 | text 文本

1 | 1 | 2 | 2 | text 文本

1 | 1 | 3 | 3 | text 文本

1 | 1 | 4 | 4 | text 文本

2 | 2 | 1 | 1 | text 文本

2 | 2 | 2 | 2 | text 文本

2 | 2 | 3 | 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. 我知道序列是自动递增的,因为它是第一个int主键,但是我只希望它自动递增,直到有一个新的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. 可以保留它,稍后再对其进行排序,但是我真的很想在每个新的testID重置序列的地方进行设置。 I'm pretty new to sqlAlchemy so I may be missing something obvious. 我是sqlAlchemy的新手,所以我可能缺少明显的东西。 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

I was able to figure this out after getting the following response from another forum . 从另一个论坛得到以下答复后,我能够弄清楚这一点。

MySQL's autoincrement feature is strictly a single integer that increases steadily. MySQL的自动增量功能严格来说是一个稳定增加的整数。 If you want an integer that is cycling, you'd need to maintain that number yourself on the Python side. 如果您想要一个循环的整数,则需要在Python端自己维护该数字。 Additionally, you'd want to set up a composite primary key on Question. 此外,您还想在Question上设置一个复合主键。 The orderinglist extension is a quick way to get the counting-per-collection effect you are looking for (works on the Python side though and it can get tripped up if you push it too far): orderinglist扩展是一种快速获得所需的每次收藏计数效果的方法(不过,该功能在Python方面有效,如果将其推得太远,它可能会被绊倒):

from sqlalchemy import * 
from sqlalchemy.orm import * 
from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy.ext.orderinglist import ordering_list 
Base = declarative_base() 


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


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

e = create_engine("sqlite://", echo=True) 
Base.metadata.create_all(e) 
s = Session(e) 


s.add_all([ 
    Test( 
        questions=[ 
            Question(text="t1q1"), 
            Question(text="t1q2"), 
            Question(text="t1q3"), 
            Question(text="t1q4"), 
        ] 
    ), 
    Test( 
        questions=[ 
            Question(text="t2q1"), 
            Question(text="t2q2"), 
            Question(text="t2q3"), 
            Question(text="t2q4"), 
        ] 
    ) 
]) 

s.commit() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM