的onupdate =" CASCADE"什么也没做

时间:2016-04-08 13:54:10

标签: sqlite sqlalchemy

尝试使用onupdate="CASCADE"但未应用级联。使用LeadLeadStatus模型,Lead.status_id需要级联:

# -*- coding: utf-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'
app.config['DEBUG'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = False
db = SQLAlchemy(app)

class Lead(db.Model):
    __tablename__ = 'lead'

    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(1024), nullable=False, server_default='')
    status_id = db.Column(db.String(16), db.ForeignKey('lead_status.id', onupdate='CASCADE'))
    status = db.relationship('LeadStatus', back_populates='leads')


class LeadStatus(db.Model):
    __tablename__ = 'lead_status'

    id = db.Column(db.String(16), primary_key=True)
    leads = db.relationship('Lead', back_populates='status')


def print_trace(lead_status, lead):
    print('DEBUG: status.id is {0}'.format(lead_status.id))
    print('DEBUG: {0}.status_id is {1}'.format(lead.name, lead.status_id))


if __name__ == '__main__':
    db.create_all()

    lead_status = LeadStatus(id='new')
    lead = Lead(name='Foo', status=lead_status)

    db.session.add(lead_status)
    db.session.add(lead)
    db.session.commit()

    # debug
    print_trace(lead_status, lead)

    lead_status = db.session.query(LeadStatus).filter(LeadStatus.id == 'new').one()
    lead = db.session.query(Lead).filter(Lead.name == 'Foo').one()

    lead_status.id = 'new_renamed'
    # db.session.delete(lead_status)
    db.session.commit()

    # debug
    print_trace(lead_status, lead)

    # lead_status = db.session.query(LeadStatus).filter(LeadStatus.id == 'new_renamed').one()
    lead = db.session.query(Lead).filter(Lead.name == 'Foo').one()

    # debug
    print_trace(lead_status, lead)

代码生成:

DEBUG: status.id is new
DEBUG: Foo.status_id is new
DEBUG: status.id is new_renamed
DEBUG: Foo.status_id is new
DEBUG: status.id is new_renamed
DEBUG: Foo.status_id is new

如何使onupdate="CASCADE"有效?

1 个答案:

答案 0 :(得分:0)

SQLite默认禁用密钥用法,请参阅https://www.sqlite.org/foreignkeys.html#fk_enable。你必须激活它们:

db.session.execute('PRAGMA foreign_keys = ON')

现在生成代码:

DEBUG: status.id is new
DEBUG: Foo.status_id is new
DEBUG: status.id is new_renamed
DEBUG: Foo.status_id is new_renamed
DEBUG: status.id is new_renamed
DEBUG: Foo.status_id is new_renamed
相关问题