Flask SQLAlchemy, '__init__ takes exactly one argument' when adding database record via class constructor
In my API I often use a pattern like this:
from app.models import Timecards
def add_timecard(payload):
new_timecard = Timecards(**payload)
db.session.add(new_timecard)
db.session.commit()
Where Timecards
is:
class Timecards(db.Model):
__tablename__ = "timesheet_line"
id = db.Column("line_id", db.Integer, primary_key=True, autoincrement=True)
description = db.Column("line_description", db.String(255))
ref_number = db.Column("line_ref_number", db.String(255))
notes = db.Column("line_notes", db.String(255))
approved_by_id = db.Column("line_approved_by_id", db.String(255))
date_approved = db.Column("line_date_approved", db.DateTime)
date_submitted = db.Column("line_date_submitted", db.DateTime)
time_started = db.Column("line_time_started", db.DateTime)
time_ended = db.Column("line_time_ended", db.DateTime)
user_id = db.Column("line_user_id", db.Integer)
is_active = db.Column("line_isActive", db.Integer)
I do not have a custom __init__
method defined on this class. All my other classes work perfectly fine when I interact with them this way but for some reason this particular class seems to be only accept this automatically passed self
argument and rejects my payload
with the error
__init__() takes exactly 1 argument (2 given)
For example I have this class:
class ServicesRendered(db.Model):
__tablename__ = "services_rendered"
id = db.Column("service_id", db.Integer, primary_key=True, autoincrement=True)
notes = db.Column("service_notes", db.String(255))
status = db.Column("service_status", db.String(255))
time_open = db.Column("service_timeOpen", db.DateTime)
time_close = db.Column("service_timeClose", db.DateTime)
is_active = db.Column("service_isActive", db.Integer)
and the following code works perfectly:
def add_services_rendered(payload):
new_services_rendered = ServicesRendered(**payload)
As far as I can tell these classes are defined in the same way, and I am interacting with them in the same way, but one throws an error and the other does not.
python flask sqlalchemy flask-sqlalchemy
|
show 1 more comment
In my API I often use a pattern like this:
from app.models import Timecards
def add_timecard(payload):
new_timecard = Timecards(**payload)
db.session.add(new_timecard)
db.session.commit()
Where Timecards
is:
class Timecards(db.Model):
__tablename__ = "timesheet_line"
id = db.Column("line_id", db.Integer, primary_key=True, autoincrement=True)
description = db.Column("line_description", db.String(255))
ref_number = db.Column("line_ref_number", db.String(255))
notes = db.Column("line_notes", db.String(255))
approved_by_id = db.Column("line_approved_by_id", db.String(255))
date_approved = db.Column("line_date_approved", db.DateTime)
date_submitted = db.Column("line_date_submitted", db.DateTime)
time_started = db.Column("line_time_started", db.DateTime)
time_ended = db.Column("line_time_ended", db.DateTime)
user_id = db.Column("line_user_id", db.Integer)
is_active = db.Column("line_isActive", db.Integer)
I do not have a custom __init__
method defined on this class. All my other classes work perfectly fine when I interact with them this way but for some reason this particular class seems to be only accept this automatically passed self
argument and rejects my payload
with the error
__init__() takes exactly 1 argument (2 given)
For example I have this class:
class ServicesRendered(db.Model):
__tablename__ = "services_rendered"
id = db.Column("service_id", db.Integer, primary_key=True, autoincrement=True)
notes = db.Column("service_notes", db.String(255))
status = db.Column("service_status", db.String(255))
time_open = db.Column("service_timeOpen", db.DateTime)
time_close = db.Column("service_timeClose", db.DateTime)
is_active = db.Column("service_isActive", db.Integer)
and the following code works perfectly:
def add_services_rendered(payload):
new_services_rendered = ServicesRendered(**payload)
As far as I can tell these classes are defined in the same way, and I am interacting with them in the same way, but one throws an error and the other does not.
python flask sqlalchemy flask-sqlalchemy
Can you show us how you're instantiatingTimecards
to get the error? Are you using the**
to explodepayload
?
– chris
Nov 20 '18 at 19:37
its in the first block of code,Timecards(**payload)
– Robbie Milejczak
Nov 20 '18 at 19:37
Ah, sorry 'bout that, scanned right past it. What's inpayload
? The error says only 2 parameters were supplied (one of which isself
, as you pointed out), doespayload
contain only one element?
– chris
Nov 20 '18 at 19:39
Please include the full trace back in your question.
– SuperShoot
Nov 20 '18 at 19:47
In your second example,payload
must be an empty dictionary, in your first, it's not. You are calling__init__(self, <whatever payload explotes into>)
and the method is defined as__init__(self)
. That's why you're getting the error.
– yorodm
Nov 20 '18 at 21:09
|
show 1 more comment
In my API I often use a pattern like this:
from app.models import Timecards
def add_timecard(payload):
new_timecard = Timecards(**payload)
db.session.add(new_timecard)
db.session.commit()
Where Timecards
is:
class Timecards(db.Model):
__tablename__ = "timesheet_line"
id = db.Column("line_id", db.Integer, primary_key=True, autoincrement=True)
description = db.Column("line_description", db.String(255))
ref_number = db.Column("line_ref_number", db.String(255))
notes = db.Column("line_notes", db.String(255))
approved_by_id = db.Column("line_approved_by_id", db.String(255))
date_approved = db.Column("line_date_approved", db.DateTime)
date_submitted = db.Column("line_date_submitted", db.DateTime)
time_started = db.Column("line_time_started", db.DateTime)
time_ended = db.Column("line_time_ended", db.DateTime)
user_id = db.Column("line_user_id", db.Integer)
is_active = db.Column("line_isActive", db.Integer)
I do not have a custom __init__
method defined on this class. All my other classes work perfectly fine when I interact with them this way but for some reason this particular class seems to be only accept this automatically passed self
argument and rejects my payload
with the error
__init__() takes exactly 1 argument (2 given)
For example I have this class:
class ServicesRendered(db.Model):
__tablename__ = "services_rendered"
id = db.Column("service_id", db.Integer, primary_key=True, autoincrement=True)
notes = db.Column("service_notes", db.String(255))
status = db.Column("service_status", db.String(255))
time_open = db.Column("service_timeOpen", db.DateTime)
time_close = db.Column("service_timeClose", db.DateTime)
is_active = db.Column("service_isActive", db.Integer)
and the following code works perfectly:
def add_services_rendered(payload):
new_services_rendered = ServicesRendered(**payload)
As far as I can tell these classes are defined in the same way, and I am interacting with them in the same way, but one throws an error and the other does not.
python flask sqlalchemy flask-sqlalchemy
In my API I often use a pattern like this:
from app.models import Timecards
def add_timecard(payload):
new_timecard = Timecards(**payload)
db.session.add(new_timecard)
db.session.commit()
Where Timecards
is:
class Timecards(db.Model):
__tablename__ = "timesheet_line"
id = db.Column("line_id", db.Integer, primary_key=True, autoincrement=True)
description = db.Column("line_description", db.String(255))
ref_number = db.Column("line_ref_number", db.String(255))
notes = db.Column("line_notes", db.String(255))
approved_by_id = db.Column("line_approved_by_id", db.String(255))
date_approved = db.Column("line_date_approved", db.DateTime)
date_submitted = db.Column("line_date_submitted", db.DateTime)
time_started = db.Column("line_time_started", db.DateTime)
time_ended = db.Column("line_time_ended", db.DateTime)
user_id = db.Column("line_user_id", db.Integer)
is_active = db.Column("line_isActive", db.Integer)
I do not have a custom __init__
method defined on this class. All my other classes work perfectly fine when I interact with them this way but for some reason this particular class seems to be only accept this automatically passed self
argument and rejects my payload
with the error
__init__() takes exactly 1 argument (2 given)
For example I have this class:
class ServicesRendered(db.Model):
__tablename__ = "services_rendered"
id = db.Column("service_id", db.Integer, primary_key=True, autoincrement=True)
notes = db.Column("service_notes", db.String(255))
status = db.Column("service_status", db.String(255))
time_open = db.Column("service_timeOpen", db.DateTime)
time_close = db.Column("service_timeClose", db.DateTime)
is_active = db.Column("service_isActive", db.Integer)
and the following code works perfectly:
def add_services_rendered(payload):
new_services_rendered = ServicesRendered(**payload)
As far as I can tell these classes are defined in the same way, and I am interacting with them in the same way, but one throws an error and the other does not.
python flask sqlalchemy flask-sqlalchemy
python flask sqlalchemy flask-sqlalchemy
asked Nov 20 '18 at 19:34
Robbie MilejczakRobbie Milejczak
2,2941626
2,2941626
Can you show us how you're instantiatingTimecards
to get the error? Are you using the**
to explodepayload
?
– chris
Nov 20 '18 at 19:37
its in the first block of code,Timecards(**payload)
– Robbie Milejczak
Nov 20 '18 at 19:37
Ah, sorry 'bout that, scanned right past it. What's inpayload
? The error says only 2 parameters were supplied (one of which isself
, as you pointed out), doespayload
contain only one element?
– chris
Nov 20 '18 at 19:39
Please include the full trace back in your question.
– SuperShoot
Nov 20 '18 at 19:47
In your second example,payload
must be an empty dictionary, in your first, it's not. You are calling__init__(self, <whatever payload explotes into>)
and the method is defined as__init__(self)
. That's why you're getting the error.
– yorodm
Nov 20 '18 at 21:09
|
show 1 more comment
Can you show us how you're instantiatingTimecards
to get the error? Are you using the**
to explodepayload
?
– chris
Nov 20 '18 at 19:37
its in the first block of code,Timecards(**payload)
– Robbie Milejczak
Nov 20 '18 at 19:37
Ah, sorry 'bout that, scanned right past it. What's inpayload
? The error says only 2 parameters were supplied (one of which isself
, as you pointed out), doespayload
contain only one element?
– chris
Nov 20 '18 at 19:39
Please include the full trace back in your question.
– SuperShoot
Nov 20 '18 at 19:47
In your second example,payload
must be an empty dictionary, in your first, it's not. You are calling__init__(self, <whatever payload explotes into>)
and the method is defined as__init__(self)
. That's why you're getting the error.
– yorodm
Nov 20 '18 at 21:09
Can you show us how you're instantiating
Timecards
to get the error? Are you using the **
to explode payload
?– chris
Nov 20 '18 at 19:37
Can you show us how you're instantiating
Timecards
to get the error? Are you using the **
to explode payload
?– chris
Nov 20 '18 at 19:37
its in the first block of code,
Timecards(**payload)
– Robbie Milejczak
Nov 20 '18 at 19:37
its in the first block of code,
Timecards(**payload)
– Robbie Milejczak
Nov 20 '18 at 19:37
Ah, sorry 'bout that, scanned right past it. What's in
payload
? The error says only 2 parameters were supplied (one of which is self
, as you pointed out), does payload
contain only one element?– chris
Nov 20 '18 at 19:39
Ah, sorry 'bout that, scanned right past it. What's in
payload
? The error says only 2 parameters were supplied (one of which is self
, as you pointed out), does payload
contain only one element?– chris
Nov 20 '18 at 19:39
Please include the full trace back in your question.
– SuperShoot
Nov 20 '18 at 19:47
Please include the full trace back in your question.
– SuperShoot
Nov 20 '18 at 19:47
In your second example,
payload
must be an empty dictionary, in your first, it's not. You are calling __init__(self, <whatever payload explotes into>)
and the method is defined as __init__(self)
. That's why you're getting the error.– yorodm
Nov 20 '18 at 21:09
In your second example,
payload
must be an empty dictionary, in your first, it's not. You are calling __init__(self, <whatever payload explotes into>)
and the method is defined as __init__(self)
. That's why you're getting the error.– yorodm
Nov 20 '18 at 21:09
|
show 1 more comment
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400294%2fflask-sqlalchemy-init-takes-exactly-one-argument-when-adding-database-rec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400294%2fflask-sqlalchemy-init-takes-exactly-one-argument-when-adding-database-rec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you show us how you're instantiating
Timecards
to get the error? Are you using the**
to explodepayload
?– chris
Nov 20 '18 at 19:37
its in the first block of code,
Timecards(**payload)
– Robbie Milejczak
Nov 20 '18 at 19:37
Ah, sorry 'bout that, scanned right past it. What's in
payload
? The error says only 2 parameters were supplied (one of which isself
, as you pointed out), doespayload
contain only one element?– chris
Nov 20 '18 at 19:39
Please include the full trace back in your question.
– SuperShoot
Nov 20 '18 at 19:47
In your second example,
payload
must be an empty dictionary, in your first, it's not. You are calling__init__(self, <whatever payload explotes into>)
and the method is defined as__init__(self)
. That's why you're getting the error.– yorodm
Nov 20 '18 at 21:09