V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Zuckonit
V2EX  ›  Python

sqlalchemy 一对多插入出错

  •  
  •   Zuckonit · 2014-06-23 13:58:26 +08:00 · 4997 次点击
    这是一个创建于 3788 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from sqlalchemy.orm import relationship
    from sqlalchemy import Column, ForeignKey
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.dialects.mysql import INTEGER, CHAR

    Base = declarative_base()
    class User(Base):
    __tablename__ = 'user'
    __table_args__ = {'mysql_engine': 'InnoDB', 'sqlite_autoincrement': True, 'mysql_charset': 'utf8'}

    id = Column(INTEGER(unsigned=True), nullable=False, autoincrement=True, primary_key=True)
    name = Column(CHAR(32), nullable=False)



    class Address(Base):
    __tablename__ = 'address'
    __table_args__ = {'mysql_engine': 'InnoDB', 'sqlite_autoincrement': True, 'mysql_charset': 'utf8'}

    id = Column(INTEGER(unsigned=True), nullable=False, autoincrement=True, primary_key=True)
    place = Column(CHAR(32), nullable=False)
    user_id = Column(INTEGER(unsigned=True), ForeignKey("user.id"), nullable=False, unique=True)
    user = relationship("User", foreign_keys=[user_id], backref="addresses")


    from sqlalchemy import create_engine
    dsn = "mysql+mysqlconnector://test:test@localhost/test?charset=utf8&use_unicode=0"
    engine = create_engine(dsn)
    Base.metadata.create_all(engine)


    from sqlalchemy.orm import sessionmaker
    s = sessionmaker(bind=engine)()
    jack = User(name='jack')
    jack.addresses = [
    Address(place='where'),
    Address(place='here')
    ]
    s.add(jack)
    s.commit()

    报错
    sqlalchemy.exc.IntegrityError: (IntegrityError) 1062 (23000): Duplicate entry '3' for key 'user_id' u'INSERT INTO address (place, user_id) VALUES (%(place)s, %(user_id)s)' {'place': 'here', 'user_id': 3}
    4 条回复    2014-06-23 14:10:10 +08:00
    smblog
        1
    smblog  
       2014-06-23 13:59:59 +08:00
    Duplicate entry '3' for key 'user_id'
    Zuckonit
        2
    Zuckonit  
    OP
       2014-06-23 14:04:54 +08:00
    @smblog 清空test数据库跑上面代码也会出错, 这个demo是在网上看的, 不知哪错了
    Zuckonit
        3
    Zuckonit  
    OP
       2014-06-23 14:09:11 +08:00
    @Zuckonit 知道哪错了, 把unique=True去掉
    wklken
        4
    wklken  
       2014-06-23 14:10:10 +08:00
    Address

    user_id = Column(INTEGER(unsigned=True), ForeignKey("user.id"), nullable=False, unique=True)

    把unique去掉, 重建表.(or alter table column user_id)

    外键使用unique, 插入第二个address时候,肯定报错
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1065 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 19:49 · PVG 03:49 · LAX 11:49 · JFK 14:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.