2017-02-07 1 views
0

현재 SQLAlchemy 데이터베이스를 생성해야하는 스크립트를 실행하고 있습니다.이 스크립트는 아래 정보로 채워야합니다. 이 DB는 Flask 기반의 작업 목록 응용 프로그램에 연결됩니다. SQLAlchemy 2.1 사용하고 있습니다. 이것은 db_create.py 스크립트입니다. 나는 데이터베이스를 만들려고 할 때마다데이터베이스를 채우려 할 때 위치 인수 오류가 발생하는 이유는 무엇입니까?

# project/db_create.py 
from views import db 
from models import Task 
from datetime import date 

# create the database and the db table 

db.create_all() 
# insert data 

db.session.add(Task("Finish this", date(2016, 9, 22), 10, 1)) 
db.session.add(Task("Finish Python", date(2016, 10, 3), 10, 1)) 

# commit the changes 
db.session.commit() 

지금 특별히 내가이 오류를 받고 있어요 : 나는 응용 프로그램 내에서 파일 중 하나는 init() 함수를 아래로 추적 한

/Users/Paul/Desktop/RealPython/flasktaskr/ENV/lib/python3 
.6/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. 
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.') 
Traceback (most recent call last): 
File "db_create.py", line 8, in <module> 
db.session.add(Task("Finish this", date(2016, 9, 22), 10, 1)) 
TypeError: __init__() takes 1 positional argument but 5 were given 

을, 어쩌면이 힘 당신에게 도움이 될 : 나는 db_create.py 파일을 실행하면

def __init__(self, name, due_date, priority, status): 
    self.name = name 
    self.due_date = due_date 
    self.priority = priority 
    self.status = status 

는 데이터베이스가 작성 될 그러나, 그것은 원래의 파일 내의 데이터로 DB를 채우는 데 실패합니다.

왜 오류가 발생하며 DB가 채워지지 않는 이유는 무엇입니까?

답변

1

당신은 예를 들어 docs를 참조 Task

대한 SQLAlchemy의 클래스 정의에 __init__() 방법을 추가해야합니다.

첫 번째 위치 인수는 항상 self (자세한 내용을 이해하기 위해 객체 지향 프로그래밍에 대해 읽음)이지만 본질적으로이 인수는 인수를 전달하고 있으며 클래스 정의가 해당 함수를 어떻게 처리해야 하는지를 알지 못합니다.

+0

예! 이것은 그 것이다. 특히 이것은 당신이 말한 것처럼'Task'를위한'__init __()'함수였습니다. 문제는 클래스 자체의 들여 쓰기입니다. – Guren

관련 문제