2016-07-01 17 views
0

Flaskモジュールで読み込んだデータベーススキーマファイルがあります。PythonのSqlite 3で外部キーを使用しているときに構文エラーが発生しました。

PRAGMA foreign_keys = 1; 

drop table if exists user; 
create table user(uid integer primary key autoincrement, 
username text not null, 
password text not null, 
email text not null); 

drop table if exists asset; 
create table asset(aid integer primary key autoincrement, 
assetname text not null, 
releasedate text, 
FOREIGN_KEY(owner) REFERENCES user); 

外部キーフィールドを削除すると正常に動作します。

エラートレース:

File "main.py", line 75, in <module> 
    create_table() 
    File "main.py", line 30, in create_table 
    conn.cursor().executescript(f.read()) 
sqlite3.OperationalError: near "(": syntax error 

私はこのスクリプトを使用していますが、私は問題があるとは思えない方法を投稿することができます。

答えて

0

FOREIGNKEYのアンダースコアをスペースで置き換える必要があります。以下の図の制約syntaxを示しています。

enter image description here

をそしてforeign-key-clause構文は次のとおりです。

enter image description here

1

@soonあなたはFOREIGN KEYキーワードの構文エラーを持って指摘したように、また、あなたが列を定義する必要があります名前(所有者)。

drop table if exists asset; create table asset(aid integer primary key autoincrement, assetname text not null, releasedate text, owner integer, FOREIGN KEY(owner) REFERENCES user);

関連する問題