0

I am trying to post to the database but I am getting a "sqlalchemy.exc.InterfaceError". I am using Flask/ flask_sqlalchemy extension.

Models.py

class Link(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    original_url = db.Column(db.String(512))
    shorten_url = db.Column(db.String(14), unique=True)
    visits = db.Column(db.Integer, default=0)
    date_created = db.Column(db.DateTime, default=datetime.now)

    #  inheritance
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.shorten_url = self.create_short_link

    def create_short_link(self):
        characters = string.digits + string.ascii_letters 
        shorten_url = ''.join(choices(characters, k=3))

        link = self.query.filter_by(shorten_url=shorten_url).first()

        if link: 
           return self.create_short_link

        return shorten_url

Routes.py

@short.route('/add_link', methods=['POST'])
def add_link():
        original_url = request.form['original_url']
        link = Link(original_url=original_url)
        db.session.add(link)
        db.session.commit()

        return render_template('link_added.html',
          new_link=link.shorten_url, original_url=link.original_url)

Traceback

sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 1 - probably unsupported type. [SQL: INSERT INTO link (original_url, shorten_url, visits, date_created) VALUES (?, ?, ?, ?)]

0

1 Answer 1

1

Instead of

self.shorten_url = self.create_short_link

try

self.shorten_url = self.create_short_link()

The error might be pointing out that a function isn't a supported type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.