2016-02-27 62 views
0

我得到一個錯誤,運行pytest創建一個由SQLAlchemy定義的對象。SQLAlchemy,創建類時無效的語法

from sqlalchemy import Table, ForeignKey, Column, Integer, String, Text, Date 
from sqlalchemy.orm import relationship 
from .constants import * 
from .core import Base 

attendsTable = Table("attends", Base.metadata, 
    Column('left_id', Integer, ForeignKey('left.id')), 
    Column('right_id', Integer, ForeignKey('right.id')) 

class User(Base): << Invalid syntax 
    __tablename__ = 'users' 

    uid = Column(Integer, primary_key=True, autoincrement=True) 
    name = Column(String, nullable=False) 
    email = Column(String, nullable=False) 
    picPath = Column(String, unique=True) 
    description = Column(Text) 

    #one-to-many relationship with groups 
    created_groups = relationship("Group", back_populates="users") 

    #many-to-many relationship with groups 
    registered_groups = relationship("Group", secondary=attendsTable, 
    back_populates="registered_users") 

    def __repr__(self): 
     return "<User(uid=%s, name=%s)>" %(self.uid, self.name) 

這裏是我的pytest:

DB_CONN_URI = 'postgresql:://groupit:[email protected]:5432/groupitdbtest' 
SessionMaker = None 
engine = None 

@pytest.fixture(scope='module') 
def postgresql_setup(): 
    from sqlalchemy.orm import sessionmaker 
    from sqlalchemy import create_engine 
    from models import model 
    SessionMaker = sessionmaker() 
    engine = create_engine(DB_CONN_URI) 
    SessionMaker.configure(bind=engine) 

def create_user(name_p, email_p, picPath_p, description_p): 
    new_user = User(name=name_p, email=email_p, picPath=picPath_p, 
         descripton=description_p) 
    return new_user 

從pytest輸出:

 @pytest.fixture(scope='module') 
    def postgresql_setup(): 
     from sqlalchemy.orm import sessionmaker 
     from sqlalchemy import create_engine 
>  from models import model 

sqlalchemy_createtables.py:15: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

> from .model import * 
E  File "/home/ubuntu/workspace/groupitapp/groupit/models/model.py", line 10 
E  class User(Base): 
E   ^
E SyntaxError: invalid syntax 

../models/__init__.py:1: SyntaxError 

這是不是與pytest或者是一個真正的錯誤,將顯示出來,當我跑我的應用程序?

回答

1

你缺少右括號

attendsTable = Table("attends", Base.metadata, 
    Column('left_id', Integer, ForeignKey('left.id')), 
    Column('right_id', Integer, ForeignKey('right.id')) 
) # < HERE 

class User(Base): 
    __tablename__ = 'users' 

    # rest of the code