我一直在試驗使用夾具來將測試數據集加載到我的Pylons/PostgreSQL應用程序中。這很好,除非它引用一個自動增量id字段時無法正確創建外鍵。導入夾具與外鍵和SQLAlchemy?
我的燈具是這樣的:
class AuthorsData(DataSet):
class frank_herbert:
first_name = "Frank"
last_name = "Herbert"
class BooksData(DataSet):
class dune:
title = "Dune"
author_id = AuthorsData.frank_herbert.ref('id')
和模型:
t_authors = sa.Table("authors", meta.metadata,
sa.Column("id", sa.types.Integer, primary_key=True),
sa.Column("first_name", sa.types.String(100)),
sa.Column("last_name", sa.types.String(100)),
)
t_books = sa.Table("books", meta.metadata,
sa.Column("id", sa.types.Integer, primary_key=True),
sa.Column("title", sa.types.String(100)),
sa.Column("author_id", sa.types.Integer, sa.ForeignKey('authors.id'))
)
當運行 「貼紙設置 - 應用程序development.ini」,SQLAlchemey報告FK價值爲 「無」,所以它顯然沒有找到它:
15:24:27,284 INFO [sqlalchemy.engine.base.Engine.0x...9f50]
CREATE TABLE authors (
id SERIAL NOT NULL,
first_name VARCHAR(100),
last_name VARCHAR(100),
PRIMARY KEY (id)
)
15:24:27,284 INFO [sqlalchemy.engine.base.Engine.0x...9f50] {}
15:24:27,291 INFO [sqlalchemy.engine.base.Engine.0x...9f50] COMMIT
15:24:27,292 INFO [sqlalchemy.engine.base.Engine.0x...9f50]
CREATE TABLE books (
id SERIAL NOT NULL,
title VARCHAR(100),
author_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY(author_id) REFERENCES authors (id)
)
15:24:27,293 INFO [sqlalchemy.engine.base.Engine.0x...9f50] {}
15:24:27,300 INFO [sqlalchemy.engine.base.Engine.0x...9f50] COMMIT
15:24:27,301 INFO [blog.websetup] Inserting initial data
15:24:27,302 INFO [sqlalchemy.engine.base.Engine.0x...9f50] BEGIN
15:24:27,309 INFO [sqlalchemy.engine.base.Engine.0x...9f50] INSERT INTO authors (first_name, last_name) VALUES (%(first_name)s, %(last_name)s) RETURNING authors.id
15:24:27,309 INFO [sqlalchemy.engine.base.Engine.0x...9f50] {'first_name': 'Frank', 'last_name': 'Herbert'}
15:24:27,320 INFO [sqlalchemy.engine.base.Engine.0x...9f50] INSERT INTO books (title, author_id) VALUES (%(title)s, %(author_id)s) RETURNING books.id
15:24:27,320 INFO [sqlalchemy.engine.base.Engine.0x...9f50] {'author_id': None, 'title': 'Dune'}
15:24:27,322 INFO [sqlalchemy.engine.base.Engine.0x...9f50] COMMIT
15:24:27,323 INFO [blog.websetup] Done
夾具文檔實際上警告這可能是一個問題:
「但是,在某些情況下,您可能需要引用一個屬性,該屬性在加載之前沒有值,如序列ID列。 (請注意,這不是由SQLAlchemy的數據層支持使用會話時。)」
http://farmdev.com/projects/fixture/using-dataset.html#referencing-foreign-dataset-classes
,這是否意味着這只是不能與SQLAlchemy的支持?或者是有可能加載數據,而無需使用SA 「會話」?其他人如何處理此問題?
是 - 請看我上面的更新日誌SA。我甚至嘗試在完全獨立的數據()語句中插入作者和書籍數據...沒有運氣 – 2010-06-03 22:42:41