Connecting to a Database in Flask Using Flask-SQLAlchemy

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 พ.ย. 2024

ความคิดเห็น • 59

  • @rishabkumar9578
    @rishabkumar9578 2 ปีที่แล้ว +4

    Thanks Anthony the on;y channel that covers SQLalchemy videos

  • @MicaelAngeles
    @MicaelAngeles 8 หลายเดือนก่อน

    oh my god, I've been stuck in the tutorial since morning, thank you for this!

  • @AbderrahmanFodili
    @AbderrahmanFodili 2 ปีที่แล้ว +1

    I know a little bit of flask but I'm more comfortable with laravel and it has ORM, so I was a bit hesitant to whether I should keep learning flask or jump to Django but your series convinced me to keep using flask

  • @SurafelRapper
    @SurafelRapper 8 หลายเดือนก่อน

    thank you very much, man you deserve more subs.

  • @GedionDereje
    @GedionDereje 5 หลายเดือนก่อน

    Thank you, Anthony.

  • @denniskiboi7059
    @denniskiboi7059 10 หลายเดือนก่อน

    The tutorials are extremely helpful. I however couldn't access the free course on the provided link. Is it still available?

  • @odelaf
    @odelaf ปีที่แล้ว +3

    When running I got RuntimeError: Working outside of application context. If it's of any help, I discover somewhere else that instead of python I had to run flask shell. It worked.

  • @obajbola9679
    @obajbola9679 ปีที่แล้ว +3

    This typically means that you attempted to use functionality that needed
    the current application. To solve this, set up an application context
    with app.app_context(). See the documentation for more information.
    Thats the error message i get when i ran db.create_all(). pls any solution how to add the app.app_context() to my code??

    • @AK-qk2bi
      @AK-qk2bi ปีที่แล้ว +11

      write before db.create_all() write from app import app, db
      app.app_context().push()
      db.create_all()

    • @ziadehab6963
      @ziadehab6963 ปีที่แล้ว

      you can add this to your __init__.py file:
      with app.app_context():
      db.create_all()
      this should solve your problem (sorry for being late.)

    • @kashyapkrishnar8139
      @kashyapkrishnar8139 ปีที่แล้ว +1

      you can also add app.app_context().push() under db = SQLAlchemy(app). This should also work

    • @abdullahomar4126
      @abdullahomar4126 ปีที่แล้ว

      thank you so much bro you saved my time :)
      @@kashyapkrishnar8139

  • @AndySomething
    @AndySomething 16 วันที่ผ่านมา

    Can anyone explain why sometimes I see people creating Class properties as:
    id = db.Column(db.Integer, primary_key=True)
    and not
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    I've seen both ways used, and I have no idea when to use either.

  • @legohistory
    @legohistory ปีที่แล้ว

    How did you activate autocomplete for SQLAlchemy in VS Code? I have no autocomplete for this package...

  • @myrov3650
    @myrov3650 2 ปีที่แล้ว +2

    when " from app import db" , shows ModuleNotFoundError: No module named 'main'? help , please

    • @collinscollins5576
      @collinscollins5576 2 ปีที่แล้ว +1

      I have the same problem,incase you find a solution please share and incase I get a solution I will share

    • @Ritikthakur2307
      @Ritikthakur2307 6 หลายเดือนก่อน

      I also hAve this problem

  • @pacoluna7969
    @pacoluna7969 2 ปีที่แล้ว

    Thankyou a lot Anthony!!!!

  • @JhieEm27
    @JhieEm27 2 ปีที่แล้ว

    Awesome tutorial! Thank you so much~

  • @Veektaw
    @Veektaw 2 ปีที่แล้ว +6

    db.create_all() no longer works for Python3.10? It keeps talking about application context

    • @SathishKumar-zp9yg
      @SathishKumar-zp9yg 2 ปีที่แล้ว +1

      Run Python
      >>> from app import app
      >>> from app import db
      >>> db.create_all()

    • @kianuyepes6510
      @kianuyepes6510 2 ปีที่แล้ว

      I also have the same problem

    • @SathishKumar-zp9yg
      @SathishKumar-zp9yg 2 ปีที่แล้ว +3

      @@kianuyepes6510 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
      app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
      db = SQLAlchemy(app)
      app.app_context().push()
      Run in terminal
      >python
      >>>from app import app
      >>>from app import db
      >>>db.create_all()
      Now it should work

    • @fahadalikhan564
      @fahadalikhan564 2 ปีที่แล้ว

      @@SathishKumar-zp9yg hi it doesn't work for me can you please help me out I have a deadline

    • @anshumangupta8095
      @anshumangupta8095 2 ปีที่แล้ว +1

      @@fahadalikhan564 write app.app_context().push() below db = SQLAlchemy(app)

  • @secretasianman2937
    @secretasianman2937 2 ปีที่แล้ว

    Is there a way to have multiple databases used?

  • @nirupamasamal9561
    @nirupamasamal9561 2 ปีที่แล้ว

    It is what I wanted! Thanks 🙏

  • @muhammadumarsotvoldiev9555
    @muhammadumarsotvoldiev9555 ปีที่แล้ว

    Thank you very much

  • @hebertoalegre1593
    @hebertoalegre1593 ปีที่แล้ว

    Thanks. It was so heapfull

  • @ahmadabdallah245
    @ahmadabdallah245 2 ปีที่แล้ว +1

    when I run db.create_all()
    I keep running into this error: RuntimeError: Working outside of application context.

    • @arianaahrafi1574
      @arianaahrafi1574 2 ปีที่แล้ว

      Me 2

    • @simonecutrona4224
      @simonecutrona4224 2 ปีที่แล้ว +3

      I had the same problem. when you import do
      import app, db, User
      and before writing anything write
      app.app_context().push()
      now you should't get any error and you can write
      User.query.all()

    • @arianaahrafi1574
      @arianaahrafi1574 2 ปีที่แล้ว

      @@simonecutrona4224 thank you veey much i have tried that it works until you create don't create an other file just for db it just works in the main file

    • @adamsmith_1337
      @adamsmith_1337 2 ปีที่แล้ว

      @@simonecutrona4224 thankssss

    • @deathtoy101
      @deathtoy101 2 ปีที่แล้ว

      @@simonecutrona4224 i dont get it, where do you put the line "import app, db, User"? is this in the code or within the command line where he typed "python"?

  • @kmishy
    @kmishy 2 ปีที่แล้ว +1

    What is benefit of using sqlalchemy ORM over directly using respective database driver?
    We learnt SQL, now why to put effort to learn sqlalchemy?

    • @pratheeku4467
      @pratheeku4467 2 ปีที่แล้ว +2

      Learning SQLAlchemy has many advantages over traditional drivers. SQLAlchemy gives more Pythonic flavor and the flow of coding is completely Object Oriented, something that most developers are dead comfortable with. Also, by just changing a line of code, one can jump multiple DBs (without even changing the entire schema or learning new syntaxes). Finally, SQLAlchemy provides compile-time checks and type safety which I argue most database drivers don't.

  • @MKSundaram
    @MKSundaram 2 ปีที่แล้ว

    Hi Sir, my program doesn't create a db.sqlite3 file, instead it creates a directory 'instances' with no files inside it. I mean it fails to create the said file. What might be to possible reason? Thanks

    • @ayasalem7056
      @ayasalem7056 ปีที่แล้ว

      same issue, please share if u found any sol

    • @yashwantmalkani9715
      @yashwantmalkani9715 ปีที่แล้ว +2

      @@ayasalem7056
      with app.app_context():
      db.create_all()

  • @TechWithManali
    @TechWithManali 7 หลายเดือนก่อน

    Hi. Can anyone help me with the connection string for the AZURE Sql db. I have been stuck on this for last three days. IT would be a great help if someone could guide me. Thanks in advance

  • @officeformat4126
    @officeformat4126 2 ปีที่แล้ว

    Unresolved attribute reference 'String' for class 'SQLAlchemy' . db.column does not recognize. Thank you

  • @yadavsunny0312
    @yadavsunny0312 ปีที่แล้ว

    from app import db
    shows
    Traceback (most recent call last):
    File "", line 1, in
    ModuleNotFoundError: No module named 'app'....pls help

    • @prettyprinted
      @prettyprinted  ปีที่แล้ว

      You probably just need to check the names of your files. I wouldn't recommend importing your db object from a file called app.py

  • @RedRose-nc9qw
    @RedRose-nc9qw ปีที่แล้ว

    How to write a trigger in sql alchemy.??

  • @akhileshverma4812
    @akhileshverma4812 2 ปีที่แล้ว

    I found an error
    flask_sqlalchemy module not found

    • @yusharthsingh1185
      @yusharthsingh1185 2 ปีที่แล้ว

      "pip install flask-sqlachemy" should help you

  • @JoseHernandez-qk5by
    @JoseHernandez-qk5by ปีที่แล้ว

    Hi
    thanks for this video... I would like to ask you if is it possible to create the table from the python file code, instead of gettign into the terminal???:
    if "__name__" == "__main__":
    db.create_all()
    app.run(debug=True)
    For this I'm using:
    "mysql://root:''@localhost:3306/testflask"
    And my laragon local server is on

  • @heximcz
    @heximcz 11 หลายเดือนก่อน

    not work without context.
    app.app_context().push()

  • @briancastroTI
    @briancastroTI 2 ปีที่แล้ว

    Thanks! Great video, very useful