我正在尝试在模型中设计这两个实体之间的关系:
Lick
是Note
的有序序列(作为回报,它可以是不同舔法的一部分),因此我决定在关联类中实现一个具有位置字段的多对多。我想为我的Lick类添加一个唯一约束,以避免重复,但是我不知道如何以ORM方式进行操作。我想出的唯一解决方案是在Lick类中设置一个自定义id字段(而不是通常的增量整数),我确定该字段是唯一的,但是我觉得我基本上是在字符串数据库中已经存在的内容。有没有一种方法可以在Lick类中设置与其音符顺序有关的唯一字段?
先谢谢了。在下面可以看到我的模型:
class LickNote(db.Model):
__tablename__ = 'lick_note'
__table_args__ = (
db.ForeignKeyConstraint(('note_string', 'note_fret', 'note_value'),
('note.string', 'note.fret', 'note.value')),
)
lick_id = db.Column(db.String, db.ForeignKey('lick.id'), primary_key=True)
note_string = db.Column(db.Integer, primary_key=True)
note_fret = db.Column(db.Integer, primary_key=True)
note_value = db.Column(db.Integer, primary_key=True)
position = db.Column(db.Integer)
notes = db.relationship("Note")
class Lick(db.Model):
"""
A lick is identified as a sequence of notes. The identifier is a string in this form:
ssffvvssffvv.....ssffvv, where ss refers to the string (00-12), ff to the fret (00-23) and vv to the value of the
note (00-11). This sequence is guaranteed to be unique
"""
__tablename__ = 'lick'
id = db.Column(db.String, primary_key=True)
notes = db.relationship("LickNote")
class Note(db.Model):
"""
Value represents the int conversion of the note [0,11]. Marked as primary to identify the possibility of having
different tunings for the same position on the fretboard
"""
__tablename__ = 'note'
string = db.Column(db.Integer, primary_key=True)
fret = db.Column(db.Integer, primary_key=True)
value = db.Column(db.Integer, primary_key=True)