SQLAlchemy Table(表)類方式 - Table類和Column類
Table 構造方法
1 Table(name, metadata[, *column_list][, **kwargs])
引數說明:
- name 表名
-
metadata
元資料物件 -
column_list
是列(Column
或其他繼承自SchemaItem
的物件)列表 -
kwargs
主要內容:-
schema
: (None
)表的模式(一般預設是資料庫名, 無需特別指定; Oracle中是owner
, 當一個數據庫由多個使用者管理時,使用者的預設資料庫不是要連線的資料庫時,需要指定此項) -
autoload
: (False
)是否自動載入 -
autoload_replace
: (True
)是否自動用元資料中載入的列替換column_list
中已經存在了的同名列-
為
True
時自動將column_list
中已經存在了的列替換為從元資料中載入的同名列 -
為
False
時會忽略元資料有,且column_list
中已經存在了的列
-
為
-
autoload_with
: 自動載入的引擎(Engine
)或連線(Connection
)物件-
為
None
時-
autoload
為True
時, 會從傳遞的metadata
中尋找引擎或連線物件
-
-
不為
None
時-
當
autoload
不為True
時,autoload
會自動被修改為True
-
當
-
為
-
comment
: 註釋 -
extend_existing
: (False
)當表已經存在於元資料中時,如果元資料中存在與column_list
中的列同名的列,column_list
中同名的列會替換掉元資料中已經有的列 -
keep_existing
: (False
)當表已經存在於元資料中時,如果元資料中存在與column_list
中的列同名的列,column_list
中同名的列會被忽略 -
include_columns
:(None
)從元資料中只需載入的表的列名列表 -
mustexist
: (False
)表名是否一定需要存在於元資料中(不存在時引發異常)
-
常用
SchemaItem
子類:
-
PrimaryKeyConstraint
-
ForeignKeyConstraint
注意,在使用不同版本的SQLAlchemy時,以上引數中:
- 老版本中可能部分引數還沒有
- 新版本中可能廢棄了部分引數
-
keep_existing
與extend_existing
互相排斥,不能同時傳遞為True
-
keep_existing
與extend_existing
適用於新建表物件;如果要建立新的表,表明已經存在於meta.tables
中時,需要指明任意一個引數,不然會報錯。 -
useexisting
已被廢棄, 新版本使用extend_existing
Column的構造方法
Column([name, ]type_[, **kwargs])
引數說明:
-
name
欄位名 -
type_
欄位資料型別,這裡的資料型別 包括:-
SQLAlchemy中常用資料型別
:
-
整數:
SmallInteger
、Integer
、BigInteger
等 -
浮點數:
Float
、Numeric
等 -
文字字串:
String
、Text
、Unicode
、UnicodeText
、CHAR
、VARCHAR
等 -
二進位制字串:
LargeBinary
、BINARY
、VARBINARY
等 -
日期時間:
Date
、DateTime
、TIMESTAMP
等
-
整數:
-
Constraint
: 約束 -
ForeignKey
: 外來鍵 -
ColumnDefault
: 列預設值
-
SQLAlchemy中常用資料型別
:
-
kwargs
主要內容 :-
autoincrement
: (False
)是否是主鍵 -
default
: (None
)預設值 -
index
: (None
)索引 -
nullable
: (True
)是否可以為空(NULL
) -
primary_key
: (False
)是否是主鍵 -
server_default
: (None
)服務端(資料庫中的函式)預設值 -
unique
: (False
)是否唯一 -
comment
: (None
)列註釋
-