1

I want to insert test data into mysql users tables. I can add data in python shell at first time, but I don't know how to insert data in a new shell environment:

MariaDB [python]> select * from users;
+----+----------+---------+
| id | username | role_id |
+----+----------+---------+
|  1 | John     |    1 |
|  2 | Mary     |    2 |
|  3 | Terry    |    3 |  
|  4 | test_user|    1 | (I want insert 'test_user' into mysql like this)

MariaDB [python]> select * from roles;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | Admin     |
|  2 | Moderator |
|  3 | User      |
+----+-----------+

[root@docker1 namecard]# /root/namecard/bin/python3 manage.py shell
>>> admin_role = Role(name='Admin')
>>> user_test = User(username='test_user', role=admin_role)
>>> db.session.add(user_test)
>>> db.session.commit()
============================================================
Traceback (most recent call last):
   File "/root/namecard/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 1 context)
... ...
sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry 'Admin' for key 'name'") [SQL: 'INSERT INTO roles (name) VALUES (%(name)s)'] [parameters: {'name': 'Admin'}] (Background on this error at: http://sqlalche.me/e/gkpj)
============================================================

I know the system report a duplicate entry Admin error, but I don't know how to insert a data into mysql table in python shell environment.

Here's my models.py file:

[root@docker1 namecard]# cat app/models.py
from . import db
class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role %r>' % self.name
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username
2
  • username field has unique attribute, this causes the error, no duplication allowed in this field. in another word, Admin already exists in the table. Commented Jun 26, 2018 at 8:00
  • thx for your help and how can I insert data into mysql table? Commented Jun 26, 2018 at 8:18

1 Answer 1

3

Your role table already has entry for Admin so you don't need to insert it once again. Edit your manage.py (or run it in shell) with these lines:

admin_role = db.session.query(Role).filter_by(name='Admin').first()
user_test = User(username='test_user', role_id=admin_role.id)
db.session.add(user_test)
db.session.commit()
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.