我有麻煩與SQL鍊金術既建立模型和選擇/插入SQLAlchemy的適當的模型定義和選擇/插入
如果我設置的模型如下,並在項目插入表,它的工作原理:
#!/usr/bin/python
import pymysql
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import select
engine = sqlalchemy.create_engine('mysql+pymysql://[email protected]/music?charset=utf8&use_unicode=0', pool_recycle=3600)
connection = engine.connect()
Base = declarative_base(bind=engine)
Base
metadata = MetaData()
directory = Table('directory', metadata,
Column('id', Integer, primary_key=True),
Column('size', Integer),
Column('name', String, unique=True),
)
words = Table('words', metadata,
Column('id', Integer, primary_key=True),
Column('source', Integer, ForeignKey('directory.id')),
Column('words', String),
)
ratios = Table('ratios', metadata,
Column('id', Integer, primary_key=True),
Column('source', Integer, ForeignKey('directory.id')),
Column('target', Integer, ForeignKey('directory.id')),
Column('ratio', Integer),
)
metadata.create_all(engine)
# ---------------------------------------------------------------------
# Execute Insert
# ---------------------------------------------------------------------
i = words.insert().values(words='jack', source=1)
result = connection.execute(i)
不過,如果我使用上述模型,並嘗試使用記錄的ID選擇(如下命令),我沒有得到任何結果:
s = directory.select().where(id == 1)
result = connection.execute(s)
for r in result:
print r
表確實有帶有該ID的記錄,所以它應該返回結果!
如果不是我設置的模型/表如下,使用選擇命令(如下),這個工程:
#!/usr/bin/python
import pymysql
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import *
from sqlalchemy import UniqueConstraint
engine = sqlalchemy.create_engine('mysql+pymysql://[email protected]/music?charset=utf8&use_unicode=0', pool_recycle=3600)
connection = engine.connect()
Base = declarative_base(bind=engine)
Base
class Directory(Base):
__tablename__ = "directory"
id = Column(Integer, primary_key=True)
name = Column(String(767), unique=True)
size = Column(Integer)
class Words(Base):
__tablename__ = "words"
id = Column(Integer, primary_key=True)
source = Column(Integer, ForeignKey('directory.id'))
words = Column(String(1500))
class Ratios(Base):
__tablename__ = "ratios"
id = Column(Integer, primary_key=True)
source = Column(Integer, ForeignKey('directory.id'))
target = Column(Integer, ForeignKey('directory.id'))
ratio = Column(Integer)
class Rename(Base):
__tablename__ = "rename"
id = Column(Integer, primary_key=True)
source = Column(Integer, ForeignKey('directory.id'))
name = Column(String(1500))
Base.metadata.create_all()
# ---------------------------------------------------------------------
# Execute Select
# ---------------------------------------------------------------------
s = select([Directory]).where(Directory.id == 1)
result = connection.execute(s)
for r in result:
print r
不過,如果我遵循類似的語法選擇一個插入,這並未」將不起作用:
i = insert([Words]).values(source=1, words=out)
connection.execute(i)
問題: 有人可以推薦創建模型的哪種風格是正確的/最佳?
有人可以澄清插入,更新,選擇推薦的模型的語法?
有沒有一個教程,你可以建議,簡潔明瞭?我一直在瀏覽SQLAlchemy文檔,這真的讓人感到困惑 - 我發現了以上兩種方式設置模型的示例,但沒有找到任何以直觀和一致的方式「端對端」的教程。
謝謝。