1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class BaseModel(Model):
id = Column(Integer, primary_key=True)
created_at = Column(DateTime(True), default=func.now(), nullable=False)
updated_at = Column(DateTime(True), default=func.now(), onupdate=func.now(), nullable=False)

@classmethod
def create(cls, **kw):
session = db.session
if 'id' in kw:
obj = session.query(cls).get(kw['id'])
if obj:
return obj
obj = cls(**kw)
session.add(obj)
session.commit()
return obj

def to_dict(self):
columns = self.__table__.columns.keys()
return {key: getattr(self, key) for key in columns}


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../cnblogblog.db'

db = SQLAlchemy(app, model_class=BaseModel)


######################################################################################################

class Parent(db.Model):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
name = Column(String(20))


class Child(db.Model):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String(20))

parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", backref=backref("child"))

### 只删除父级,子不影响
# 1. parent_id = Column(Integer, ForeignKey('parent.id', ondelete="CASCADE"))
# parent = relationship("Parent", backref=backref("child", passive_deletes=True))

### 子级跟随删除
# 2. parent = relationship("Parent", backref = backref("child", cascade="all, delete-orphan"))
# 3. parent = relationship("Parent", backref = backref("child", cascade="all,delete"))

## 父级删除,子级不删除,外键更新为 null
# 4. parent = relationship("Parent", backref = backref("child"))


if __name__ == '__main__':
db.create_all()
Parent.create(name='ZhangTian')
Parent.create(name='LiTian')
Child.create(name='ZhangDi', parent_id=1)
Child.create(name='LiDi', parent_id=2)

parent = db.session.query(Parent).first()
db.session.delete(parent)
db.session.commit()

`