Pylons 1.0 and SQLAlchemy 0.6 - How do I Model?
I've been reading http://pylonsbook.com/en/1.1/starting-the-simplesite-tutorial.html and following along with their SimpleSite tutorial but am having some issues with creating the Model.
The Model imports they use on the tutorial are:
"""The application's model objects"""
import sqlalchemy as sa
from sqlalchemy import orm
from simplesite.model import meta
# Add these two imports:
import datetime
from sqlalchemy import schema, types
They then use this to create a table:
page_table = schema.Table('page', meta.metadata,
Though, when I try that, I get:
开发者_开发知识库AttributeError: 'module' object has no attribute 'metadata'
I'm guessing Pylons changed their ways during the version upgrade...
So how do I do this? Can someone link me to an updated tutorial on making a Model and handling database connections/queries? :/
Pylons 1.0 use declarative Base as default to model.
exemple:
from sqlalchemy import Column
from sqlalchemy.types import Integer, Unicode,
from MYPROJECT.model.meta import Base
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(Unicode(100))
You can see the updated reference in http://pylonshq.com/docs/en/1.0/models/
I think you should use Base.metadata instead of meta.metadata for Pylons 1.0.
It should be something like that:
from blog.model.meta import Session, Base
article_table = sa.Table("article", Base.metadata,
sa.Column("id", sa.types.Integer, primary_key=True),
sa.Column("lang", sa.types.String(255), nullable=False),
)
精彩评论