Monday - November 14, 2011language: Englishposted by: Blackmore
Monday - November 14, 2011language: Englishposted by: Blackmore
-- in PostgreSQL --
CREATE SCHEMA my_books;
CREATE TABLE my_books.books (
...
);
-- CREATE TABLE books ... will create the table books in the schema "public"
# SQLAlchemy
## using the "classical" method with mapper()
metadata = MetaData()
authors_table = Table('authors', metadata,
Column('author_id', Integer,
Sequence("custom_seq_author_id",
schema='my_books'),
primary_key=True),
Column('name', String(128)),
schema='my_books')
books_table = Table('books', metadata,
Column('book_id', Integer,
Sequence("custom_seq_book_id", schema='my_books'),
primary_key=True),
Column('title', String(256)),
Column('author_id',
ForeignKey("my_books.author_id")), #N.B.
schema='my_books')
# ...
# mapper(Book, books_table)
# mapper(Author, authors_table)...
## using the declarative method in SQLAlchemy
Base = declarative_base()
class Book(Base):
__tablename__ = "books"
__table_args__ = {'schema':'my_books'}
book_id = Column(Integer,
Sequence("custom_seq_book_id", schema='my_books'),
primary_key=True),
title = Column(String(256))
author_id = Column(ForeignKey("my_books.author_id")) #N.B.