sqlalchemy的ORM的Column常用参数

default:默认值

nullable:是否为空

primary_key:主键

unique:是否唯一

autoincrement:是否自增

onupdate:更新时执行的

name:数据库映射后的属性

from flask import Flask, jsonify, json
from sqlalchemy import create_engine, Column, Integer, String, DateTime

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from datetime import datetime

# 连接数据库字符串
DB_URI = "mysql://root:[email protected]/pyDemo"

engine = create_engine(DB_URI)
# 判断是否连接成功
engine.connect()

# 创建ORM
Base = declarative_base(engine)

class Article(Base):
    # 表名字
    __tablename__ = 'Article'
    # 列
    id = Column(Integer, primary_key=True, autoincrement=True)
    read_count = Column(Integer, default=0)
    title = Column(String(50), name="my_title", default='1111')
    create_time = Column(DateTime, default=datetime.now)
    update_time = Column(DateTime, onupdate=datetime.now, default=datetime.now)


# 生成实体
Base.metadata.drop_all()
Base.metadata.create_all()

个人博客:十分钟空间:https://tmspace.cn/

更多内容关注公众号:tenmspace

你可能感兴趣的:(flask开发web网站)