简体   繁体   中英

SQLAlchemy, invalid syntax when creating class

I am getting an error running pytest to create an object defined by 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)

Here is my pytest:

DB_CONN_URI = 'postgresql:://groupit:hx8889@localhost: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

Output from 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

Is this something with pytest or is it a real error that will show up when I run my app?

You are missing the closing parenthesis :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM