簡體   English   中英

sqlalchemy數據庫創建錯誤

[英]sqlalchemy database creation error

我正在處理一個項目,但遇到一個小錯誤,該錯誤使我無法創建所有表和數據庫。 我收到以下錯誤:

vagrant@vagrant-ubuntu-trusty-32:/vagrant/PayUp$ python setup_database.py
Traceback (most recent call last):
  File "setup_database.py", line 22, in <module>
    class Users(Base):
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/api.py", line 50, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/ext/declarative/base.py", line 227, in _as_declarative
    if not table.c.contains_column(c):
AttributeError: 'str' object has no attribute 'c'

我正在使用的代碼如下:

#The following are all of the standard imports that are needed to run the database
import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String, Index
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

#The following is what will create the declarative_base base that will be imported to every tables
Base = declarative_base()

#The following is the user table which will store the users id and username
class Users(Base):
    __table__ = 'users'

    id = Column(Integer, primary_key = True)
    user_name = Column(String(16), nullable = False, unique = True, index = True)

class User_Auth(Base):
    __table__ = 'user_auth'

    id = Column(Integer, primary_key = True)
    last_name = Column(String(16), nullable = False)
    first_name = Column(String(16), nullable = False)
    password = Column(String(225), nullable = False)

class User_Info(Base):

    __table__ = 'user_info'

    id = Column(Integer, primary_key = True)
    email = Column(String(50), nullable = False, index = True, unique = True)
    phone = Column(Integer(12), nullable = True, unique = True)
    age = Column(Integer(2), nullable = False)
    gender = Column(String(2), nullable = True)

class User_Location(Base):
    __table__ = 'user_location'

    id = Column(Integer, primary_key = True)
    street = Column(String(200), mullable = False)
    city = Column(String(35), nullable = False)
    state = Column(String(3), nullable = False)
    zip_code = Column(Integer(5), nullable = False, index = True)


engine = create_engine('sqlite:///payup.db')

Base.metadata.create_all(engine)

您是否嘗試過將__table__替換為__tablename__

http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/table_config.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM