sqlalchemy的ORM的Column常用参数

2020-04-12  本文已影响0人  深夜程序员_

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:123456@127.0.0.1/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()

上一篇下一篇

猜你喜欢

热点阅读