使用sqlalchemy的ORM建立外來鍵關聯時報錯

2021-09-21 04:50:15 字數 3600 閱讀 9974

在學習使用sqlalchemy模組的時候踩了乙個坑,分享一下。

我先用下面的語句建立了一張學生資訊表:

> create table student (

-> id int unsigned auto_increment,

-> name varchar(20) not null,

-> age tinyint,

-> primary key (id)

-> );

表裡就3個字段:自增id(無符號的數字,自增id不會是負數,當然用無符號,感覺自己好專業),name 和 age(話說這個也應該是無符號)。

在學習了mysql之後,學習了一下pymysql 模組。最後是這個orm,sqlachemy。用這個表測試了很多命令和知識點,然後進行到使用sqlalchemy建立外來鍵關聯的時候卡住了。之前也有sql語句建過外來鍵關聯,也很順利。

現在已有一張學生資訊表,再建立一張學生成績表。3個字段:考試名稱、學生id、成績。考試名和學生id作為復合主鍵,學生id關聯資訊表中的id,做外來鍵關聯:

from sqlalchemy import create_engine

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import column, integer, string

from sqlalchemy import foreignkey # 外來鍵關聯需要這個

engine = create_engine("mysql+pymysql:",

encoding='utf-8', echo=true)

base = declarative_base() # 生成orm基類

class

student

(base):

__tablename__ = 'student'

# 表名,這張表不建立,可以寫的簡單點

id = column(primary_key=true) # 只要宣告你需要的欄位名,主鍵必須宣告

name = column() # 字段型別可以不要,我們不是建立表

age = column()

class

exam

(base):

__tablename__ = 'exam'

name = column(string(32), primary_key=true)

student_id = column(integer(), foreignkey("student.id"), primary_key=true) # 宣告外來鍵關聯

score = column(integer, nullable=false) # 規定不能為空

base.metadata.create_all(engine) # 建立表

讓後,報錯了。開啟echo,首先是sql語句能正常生成了:

create

table exam2 (

name

varchar(32) not

null,

student_id integer

notnull,

score integer

notnull,

primary key (name, student_id),

foreign key(student_id) references student (id)

)

之後是長長的報錯內容,就看最後一行:

sqlalchemy.exc.internalerror: (pymysql.err.internalerror) (1005, "can't create table 'week12.exam2' (errno: 150)") [sql: '\ncreate table exam2 (\n\tname varchar(32) not null, \n\tstudent_id integer not null, \n\tscore integer not null, \n\tprimary key (name, student_id), \n\tforeign key(student_id) references student (id)\n)\n\n'] (background on

this error at:

這次的關鍵字提煉比較準確,找到的內容看上去都比較有用。

首先找到的是這個原因:「外來鍵重複,刪除該錶外來鍵「。一想,之前用sql語句練習外來鍵的時候已經關聯了乙個表了,這次要拿另外乙個表關聯這個鍵,是不是就關聯不上了。我就平時乙個只用過幾個select命令的新手,沒理解那麼深啊。於是一頭跳進這個坑又轉悠了一番,最後還算好被我跳出來了。

from sqlalchemy.dialects.mysql import integer

id = column(integer(unsigned=true), primary_key=true)

找對了問題,最後自然就迎刃而解了。要建立表的**如下:

from sqlalchemy import create_engine

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import column, integer, string

from sqlalchemy import foreignkey # 外來鍵關聯需要這個

from sqlalchemy.dialects.mysql import integer # 要使用無符號的整數

engine = create_engine("mysql+pymysql:",

encoding='utf-8', echo=true)

base = declarative_base() # 生成orm基類

class

student

(base):

__tablename__ = 'student'

# 表名,這張表不建立,可以寫的簡單點

id = column(primary_key=true) # 只要宣告你需要的欄位名,主鍵必須宣告

name = column() # 字段型別可以不要,我們不是建立表

age = column()

class

exam

(base):

__tablename__ = 'exam'

name = column(string(32), primary_key=true)

student_id = column(integer(unsigned=true), foreignkey("student.id"), primary_key=true) # 宣告外來鍵關聯

score = column(integer, nullable=false) # 規定不能為空

base.metadata.create_all(engine) # 建立表

難怪講課演示的好好的,我抄來自己試就不對。我一般嫌用的例子不好,自己會改一改(演示的例子中用的都是標準的int型別)。然後就會出這種么蛾子。不過還好,自己能爬出來。

flask 與 SQLAlchemy的使用

安裝模組 pip install flask sqlalchemy在單個python中與flask使用 檔名 manage.py from flask sqlalchemy import sqlalchemy from flask import flask user root password ro...

SQLAlchemy模組的使用教程

資料庫表是乙個二維表,包含多行多列。把乙個表的內容用python的資料結構表示出來的話,可以用乙個list表示多行,list的每乙個元素是tuple,表示一行記錄,比如,包含id和name的user表 1 michael 2 bob 3 adam python的db api返回的資料結構就是像上面這...

sqlalchemy 中 desc 的使用

是這樣 items item.query.order by item.date.desc all 而不是這樣 items item.query.order by desc item.date all 更不是說了一大堆sqlalchemy的查詢方式別人看半天也沒找到自己需要的。sqlalchemy 中...