-2

So, here's my problem

i am making an application on Flask using the SQLAlchemy ORM

now the problem is, i may have messed up the creating of a table user;

in the models.py the code looks like,

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    username = db.Column(db.String(25), unique=True)
    password = db.Column(db.String(50))
    email = db.Column(db.VARCHAR(50), unique=True)
    registerdate= db.Column(db.DateTime) 

    def __init__(self,
        name,
        username,
        password,
        email,
        registerdate=None):
        self.name = name 
        self.username = username
        self.password = password
        self.email = email
        if registerdate is None:
            registerdate = datetime.utcnow()
        self.registerdate = registerdate

Now, the error is something like

OperationalError: table User has no column named user_name

this is because i messed up the table creation, creating the table with the column "user_name" first, when it gave me an error related to the underscores, i tried to modify the code but instead ran into another error...

so how do i delete the previous 'User' table in SQL Alchemy ORM without using the usual sqlite3 syntax and commands?

P.S : I am using the ubuntu 16.04 python terminal, no IDE like Atom or Pycharm and stuff ...

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Muhammad Yasir
  • 417
  • 5
  • 12
  • @IljaEverilä Possible, but i did not really find much help there. plus this is supposed to be beginner friendly – Muhammad Yasir Oct 15 '17 at 17:20
  • @IljaEverilä i wasnt working with indepth sqlite just using it as db, i'm quite new to this and didn't have much experience. i thought this would help similar users. The syntax in the question you mention was quite foreign to me – Muhammad Yasir Oct 16 '17 at 18:10
  • It seems you should step back and read the SQLAlchemy tutorials for Core and ORM found in the [docs](http://docs.sqlalchemy.org/en/latest/), and read them well. – Ilja Everilä Oct 17 '17 at 09:25

1 Answers1

-1

Alright ! so after being confused by 'engine' and a ton of other technicalities regarding sqlite,

i finally have the solution !

First enter python terminal through Ubuntu Terminal... and do,

    from YourProject import app,db
    from YourProject.models import Table1,Table2....User  #import the classes whose data you want to manipulate
    import sqlite3
    con = sqlite3.connect('ABSOLUTE PATH TO YOUR DB FILE')
    c = con.cursor()
    c.execute("DROP TABLE User;")
    db.session.commit()                     #i am a bit unsure about this one

and that's it, that's how i deleted my troublesome 'User' table. I then created a new one and it works wonders !

Also, my User Class code was previously not too well formatted, as in

def __init__(self,name,username,password,email,registerdate=None):
    self.name = name 
    self.username = username
    self.password = password
    self.email = email
    if registerdate is None:
        registerdate = datetime.utcnow()
    self.registerdate = registerdate

notice, how the Class parameters weren't in the 'stairs' formation before? that also bugged me ALOT in creating the table and adding data to it. so make sure you take care of that.

This is an issue beginners might come across and find daunting, hope i help someone out !

Cheers !

Muhammad Yasir
  • 417
  • 5
  • 12
  • Your manual connection/cursor pair are unrelated to Flask-SQLAlchemy's session, so `db.session.commit()` is redundant. – Ilja Everilä Oct 15 '17 at 17:49
  • Also, the engine you're probably referring to is not an "sqlite technicality", but a part of SQLAlchemy. Start by reading the two "Read this first" topics [here](http://docs.sqlalchemy.org/en/latest/). – Ilja Everilä Oct 17 '17 at 09:21