对于想了解flask:sqlalchemy.exc.ProgrammingError:的读者,本文将是一篇不可错过的文章,我们将详细介绍psycopg2.ProgrammingError关系“用户”不
对于想了解flask:sqlalchemy.exc.ProgrammingError:的读者,本文将是一篇不可错过的文章,我们将详细介绍psycopg2.ProgrammingError关系“用户”不存在,并且为您提供关于8PRO102 – Programming Language、Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class、C++ Programming Language中的narrow_cast实现、Django / Heroku部署(/关系“ posts_post”处的ProgrammingError不存在第1行)的有价值信息。
本文目录一览:- flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“用户”不存在(sql用户或组角色已经存在)
- 8PRO102 – Programming Language
- Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class
- C++ Programming Language中的narrow_cast实现
- Django / Heroku部署(/关系“ posts_post”处的ProgrammingError不存在第1行)
flask:sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)关系“用户”不存在(sql用户或组角色已经存在)
作为tut的一部分,我试图连接到postgres服务器,其结构如屏幕截图所示。我添加了一个db“烧瓶”,您可以看到。
基于该tut,我的主文件(’routes.py’)中包含以下代码:
from flask.ext.sqlalchemy import SQLAlchemyfrom flask import Flaskapp = Flask(__name__)app.config[''SQLALCHEMY_DATABASE_URI''] = "postgresql://postgres:123@localhost/flask"db = SQLAlchemy(app)from models import User# db.init_app(app)db.create_all()db.session.commit()admin = User(''admin'', ''admin@example.com'', ''admin1'', ''admin1@example.com'')guest = User(''admi2'', ''admin@ex1ample.com'', ''admin'', ''admin2@example.com'')# guest = User(''guest'', ''guest@example.com'')db.session.add(admin)db.session.add(guest)db.session.commit()
models.py:
from flask.ext.sqlalchemy import SQLAlchemyfrom werkzeug import generate_password_hash, check_password_hashdb = SQLAlchemy()class User(db.Model): __tablename__ = ''users'' uid = db.Column(db.Integer, primary_key = True) firstname = db.Column(db.String(100)) lastname = db.Column(db.String(100)) email = db.Column(db.String(120), unique=True) pwdhash = db.Column(db.String(54)) def __init__(self, firstname, lastname, email, password): self.firstname = firstname.title() self.lastname = lastname.title() self.email = email.lower() self.set_password(password) def set_password(self, password): self.pwdhash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.pwdhash, password)
运行时,调试器给出:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "users" does not existLINE 1: INSERT INTO users (firstname, lastname, email, pwdhash) VALU... ^ [SQL: ''INSERT INTO users (firstname, lastname, email, pwdhash) VALUES (%(firstname)s, %(lastname)s, %(email)s, %(pwdhash)s) RETURNING users.uid''] [parameters: {''lastname'': ''Admin@Example.Com'', ''firstname'': ''Admin'', ''pwdhash'': ''pbkdf2:sha1:1000$eZvJNKHO$64f59c34364e3d6094d126fa3ca2b327ab39e302'', ''email'': ''admin1''}]
我究竟做错了什么?
答案1
小编典典从本质上讲,您将需要将其拆分为几个文件,以防止导入问题并使事情更加整洁。我已经完成了以下工作。注意,我已经使用了SQLite,因为我没有在此框中安装Postgres。
app.py
from flask import Flaskapp = Flask(__name__)app.config[''SQLALCHEMY_DATABASE_URI''] = ''sqlite:////test11.db''
models.py
from flask.ext.sqlalchemy import SQLAlchemyfrom app import appdb = SQLAlchemy(app)class User(db.Model): __tablename__ = ''users'' uid = db.Column(db.Integer, primary_key = True) firstname = db.Column(db.String(100)) lastname = db.Column(db.String(100)) email = db.Column(db.String(120), unique=True) pwdhash = db.Column(db.String(54))def __init__(self, firstname, lastname, email, password): self.firstname = firstname.title() self.lastname = lastname.title() self.email = email.lower() self.set_password(password)def set_password(self, password): self.pwdhash = (password)def check_password(self, password): return password
route.py
from models import User, dbdb.create_all()db.session.commit()admin = User(''admin'', ''admin@example.com'', ''admin1'', ''admin1@example.com'')guest = User(''admi2'', ''admin@ex1ample.com'', ''admin'', ''admin2@example.com'')db.session.add(admin)db.session.add(guest)db.session.commit()
我绝对建议查看一些教程!您将需要它:您应该了解Web漏洞,最佳实践等。
8PRO102 – Programming Language
8PRO102 – Programming Language (C++)
1
Project Week #2
Rules
• The project must be developed in a team (3-6 members)
• Using code from Internet is not allowed (Don’t take a premade project from internet).
• Comment your additional files.
• Choose one of the two projects (one is simpler but gives fewer points).
• Write a report describing the structure of your project (follow the template provided). Everything
must be explained in your report.
Project A
• Implement a meeting management application.
• The application allows the user to:
o Add, remove, update, show (information) people. A person has:
An ID (composed by a 4-digit number (years of registration), and a counter that
increases each time one person is registered, 20221 for example)
A first name and a last name.
A status (Professor, Student, Administrative).
o Add, remove, update show (information) working rooms. Workings rooms have:
An ID (a number)
Maximum capacity
Remote meeting (Visio telephony) system or not
o Schedule meetings (by adding people to working rooms and setting a period). A meeting
has:
A period.
A list of participants.
The need for a visio telephony system or not
o Cancel meetings.
o A meeting that does not need visio telephony system can be set in a room that proposes
one. However, a meeting that needs a visio telephony system can not be set in a room
that does not propose one.
o Warn the user if a working room is not available for a specific period.
• Save and load from a file.
• Show credits.
• If the user tries to interrupt the program by pressing (CTRL + C) the application should asked the
user if they want to save before).
8PRO102 – Programming Language (C++)
2
• If a room is removed, and a meeting booked the room, the system asks the user to confirm. If the
user confirms all the meeting linked to this room are removed.
• If the user tries to add more participants than the room capacity, the system raises a message
and propose another room is one is available or another period.
Features Descriptions Score
Menu Main menu is show and can quit the program 1
Person Can add, remove, update and show a person, show option
prints all information about one person.
1
Working room Can add, remove, update and show a working room. Show
option prints, the ID of the room, maximum capacity, if the
room contains a visio telephony system or not, the number of
meetings linked to the meeting room
2
Meeting Can add, remove, update and show a meeting. Show option
prints, the ID of the room, the list of participants, the period, the
need for visio telephony system or not.
2
Save and load Can save and load from file (one or several, you can use JSON,
https://github.com/nlohmann/json)
2
Interrupt If the user press (CTRL + C) the application, ask if you want to
save before.
1
Credit The program show the names of all members of the group 1
Modulization Your project use headers files 1
Feedback Each action generates feedback to inform if the action has
been completed or not
1
Total 12
Welcome to Meeting Management Application
0 : Quit
1 : People menu
2 : Room menu
3 : Meeting menu
4 : Save/Load menu
5 : Credit
FIGURE 1: MAIN MENU
8PRO102 – Programming Language (C++)
3
People menu
0 : Back to main menu
1 : Register
2 : Show
3 : Update
People update menu.
Enter person ID:
20221
What do you want to update?
0: Cancel and back
1: First name
2: Last name
3: Status
Meeting menu
0 : Back to main menu
1 : Add
2 : Show
3 : Update
FIGURE 2: PEOPLE MENU
FIGURE 3: PEOPLE UPDATE MENU
FIGURE 4: MEETING MENU.
8PRO102 – Programming Language (C++)
4
Project B
• Implement a “Snake X Game of Life” game.
• “Snake X Game of Life”’s rules
o a two-dimensional orthogonal grid (400x400) of square cells
o each cell is in one of four possible states, alive (eatable, or rotten), dead, or wall.
o every cell interacts with its eight neighbours:
Any alive(rotten) cell with fewer than two live neighbours dies.
Any alive cell with two or three live neighbours lives on to the next generation.
Any alive(rotten) cell with more than three live neighbours dies on to the next
generation.
Any dead cell with exactly three live neighbours becomes alive(eatable) on to
the next generation. An eatable cell born with a counter starting at 10.
Each time an eatable cell should die, the counter is decreased by 1. If the
counter reaches 0 the eatable cell is transformed into a rotten cell.
o The snake can eat alive cells.
o If the snake eats an eatable cell, its tail grown by one cell, the user owns 3 points.
o If the snake eats a rotten cell, its tail loses one cell. The user owns 1 point. If the length
of the tail is 0 the game is over.
o If the snake head hit a wall, or its tail the game is over.
o The snake’s head and tails are considered as alive cells.
o Wall are set a random position at the beginning of the game. All are considered as dead
cells.
Room booking menu.
Do you need a visio telephony system? (y: yes, n: no)
y
How many people?
16
When? (MMDDHHmm)
03111500
When? (MMDDHHmm)
03111500
Time in minutes
60
Rooms: 101, 102, 202, 301 are available. Which one would you like?
FIGURE 5: BOOKING MENU
8PRO102 – Programming Language (C++)
5
o The player can use WASD keys to move the snake.
o The snake starts with a tail tree size length.
o The score is saved and loaded from a file each time we launch and quit the game.
Features Descriptions Score
Graphics You draw the game by using the console, (1 point),
or you use https://www.raylib.com/ (3 points)
The head and tails should have different form/colors
3
Move the snake Can move the snake using WASD keys 3
Score The score is updated each time the head eats an eatable cell 1
Tail size The tail is updated each time the head eats an eatable cell 2
Save and load Can save and load from file 1
Cells Cells follow game of life’s rules with new rules 1
Credit The program shows the names of all members of the group 1
Modulization Your project use headers files 1
Snake rules The game stops if the snake’s head hits his tail or wall cells 1
End of game Player’s score and Top score (10) is showed at the end of the
game. The users can enter their name.
2
Menu The player can start, view scores, or quit the game 1
Pause The game can be paused, and resumed 1
Cells preview The users can see a preview of next grid’s state if they don’t do
anything
2
Total 20
The following table shows optional features you can implement. You have to complete all core features
before implementing the following features.
Features Descriptions Score
Two players mode The game can be played with another player using arrows key.
Player one, starts on the left, player two on the right side of the
screen.
2
Network Two players mode using network 6
AI mode Two players mode where the second played is controlled by an
AI
4
Customization Players can customize the look of snakes (head, and tail) 1
Grid size Players can change during runtime the size of the grid 1
Total 14
Naming conventions and additional rules
- Each cpp or header files much start with a “/**/” comment containing.
a. The name of the file
8PRO102 – Programming Language (C++)
6
b. Names and ID of all members of the group
c. Names of persons who work on the file. - Each variable, and function starts with lower case.
- Classes, structures, and unions type start with upper case.
- No chunk of code copied, pasted from internet.
WX:codinghelp
Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class
Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class
A well-architected application has separate layers so different concerns don’t interact more than needed. Imagine you’re designing a loosely coupled and maintainable application,but in the middle of the development,you see some requirements that might not fit well in the architecture,such as:
- The application must have an authentication system,used before any query or update.
- The data must be validated before it’s written to the database.
- The application must have auditing and logging for sensible operations.
- The application must maintain a debugging log to check if operations are OK.
- Some operations must have their performance measured to see if they’re in the desired range.
Any of these requirements need a lot of work and,more than that,code duplication. You have to add the same code in many parts of the system,which goes against the “don’t repeat yourself” (DRY) principle and makes maintenance more difficult. Any requirement change causes a massive change in the program. When I have to add something like that in my applications,I think,“Why can’t the compiler add this repeated code in multiple places for me?” or,“I wish I had some option to ‘Add logging to this method.’”
The good news is that something like that does exist: aspect-oriented programming (AOP). It separates general code from aspects that cross the boundaries of an object or a layer. For example,the application log isn’t tied to any application layer. It applies to the whole program and should be present everywhere. That’s called a crosscutting concern.
AOP is,according to Wikipedia,“a programming paradigm that aims to increase modularity by allowing the separation of crosscutting concerns.” It deals with functionality that occurs in multiple parts of the system and separates it from the core of the application,thus improving separation of concerns while avoiding duplication of code and coupling.
In this article,I’ll explain the basics of AOP and then detail how to make it easier by using a dynamic proxy via the Microsoft .NET Framework class RealProxy.
C++ Programming Language中的narrow_cast实现
在C++中,各种数值类型的转化是C++编译过程中警告的主要来源,但是,很多时候,我们需要使用各种数值类型,例如我们用数组的某一位表示大小为对应序号的值,这种情况下,经常会涉及多种数值类型。根据C++ Programming Language中的建议,在数值类型转换时,使用narrow_cast来实现运行时安全,这里给出C++14版本的实现。
// there is no implicit conversion from Source to Target
template <typename Target, typename Source,
typename = std::enable_if_t<
!std::is_same<std::common_type_t<Target, Source>, std::decay_t<Target>>::value>>
inline Target narrow_cast(Source v)
{
static_assert(!std::is_reference<Target>::value, "The target couldn''t be reference");
static_assert(std::is_arithmetic<Source>::value, "The parameter of narrow_cast should be arithmetic");
static_assert(std::is_arithmetic<Target>::value, "The return value of narrow_cast should be arithmetic");
// using Target_U = std::remove_reference_t<Target>;
// using Source_U = std::remove_reference_t<Source>;
auto r = static_cast<Target>(v);
if (static_cast<Source>(r) != v)
throw std::runtime_error("narrow_cast<>() failed");
return r;
}
// there is implicit conversion from Source to Target
template <typename Target, typename Source,
typename = std::enable_if_t<
std::is_same<std::common_type_t<Target, Source>, std::decay_t<Target>>::value>>
inline constexpr std::remove_reference_t<Source> narrow_cast(Source v)
{
static_assert(!std::is_reference<Target>::value, "The target couldn''t be reference");
static_assert(std::is_arithmetic<Source>::value, "The parameter of narrow_cast should be arithmetic");
static_assert(std::is_arithmetic<Target>::value, "The return value of narrow_cast should be arithmetic");
return static_cast<Target>(v);
}
下面给出,使用Catch写的简单测试用例:
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include <cmath>
TEST_CASE("Test narrow_cast", "[narrow_cast]")
{
int i = 10;
long long j = 15;
long long& k = j;
REQUIRE(narrow_cast<long>(k) == 15);
REQUIRE(narrow_cast<long>(i) == 10);
long long very_big = pow(10, 12);
bool exception = false;
try
{
narrow_cast<long>(very_big) == very_big;
}
catch (const std::runtime_error& error)
{
exception = true;
}
REQUIRE(exception);
//REQUIRE(narrow_cast<long&>(k) == 15);
//REQUIRE(narrow_cast<long&>(i) == 10);
}
测试可知,在转化的类型可以容纳时,narrow_cast可以正常运行,如果narrow_cast转化后的值与原值不同时,会抛出runtime_error的异常。
Django / Heroku部署(/关系“ posts_post”处的ProgrammingError不存在第1行)
我在这里没有太多细节,但是不存在关系意味着未创建表。
因此,我怀疑在部署到heroku时,您创建了一个新数据库,但尚未为其运行迁移。
因此,请尝试在heroku数据库上运行python manage.py migrate
命令。
打开 heroku 命令行: heroku 运行 bash
进行数据库迁移: py manage.py makemigrations
迁移数据库: py manage.py 迁移
创建超级用户: py manage.py createsuperuser
以管理员身份登录您的托管站点: https://网站网址/管理员
这不会将您的数据上传到本地服务器。
您可以通过在 heroku 服务器上创建超级用户来添加数据
今天的关于flask:sqlalchemy.exc.ProgrammingError:和psycopg2.ProgrammingError关系“用户”不存在的分享已经结束,谢谢您的关注,如果想了解更多关于8PRO102 – Programming Language、Aspect-Oriented Programming : Aspect-Oriented Programming with the RealProxy Class、C++ Programming Language中的narrow_cast实现、Django / Heroku部署(/关系“ posts_post”处的ProgrammingError不存在第1行)的相关知识,请在本站进行查询。
本文标签: