SQLAlchemy完整性错误:列不是唯一的

时间:2014-11-18 13:56:16

标签: python sqlalchemy integrity

我将json数据加载到SQLAlchemy创建的数据库中:

with open('test_data.json') as f:
data=f.read()
jsondata=json.loads(data)


for row in jsondata['rows']:
    r = Review(row['business_id'])


session.add(r)
session.commit()

我将json文件的业务ID存储到business_id列中,但我总是得到这种完整性错误:

sqlalchemy.exc.IntegrityError: (IntegrityError) column business_id is not unique u'INSERT INTO    rev (business_id) VALUES (?)' (u'vcNAWiLM4dR7D2nwwJ7RPCA',)

5 个答案:

答案 0 :(得分:0)

您正尝试将已存在的值插入已标记为唯一的列business_id中。每一行都需要在此字段中具有唯一值。

答案 1 :(得分:0)

我认为您可以使用merge()而不是add()来在数据库中创建Review对象。 查看docs

答案 2 :(得分:-1)

这是我的评论表:

class Review(Base):
    __tablename__ = 'rev'

    id=Column(Integer,primary_key=True)
    business_id = Column(String(50))


def __init__(self,new_id):
    self.business_id=new_id

答案 3 :(得分:-1)

我使用sqlalchemy创建的整个数据库脚本是

import os
import sys
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine

Base = declarative_base()

class Review(Base):
    __tablename__ = 'rev'

    id=Column(Integer,primary_key=True)
    business_id = Column(String(50))



def __init__(self,new_id):
    self.business_id=new_id



engine = create_engine('sqlite:///sqlalchemy_try.db')

Base.metadata.create_all(engine)

这是我在整个数据库中将business_id从json文件加载到我的列(business_id)的整个脚本。

from sqlalchemy.exc import IntegrityError
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from sqlalchemy_declarative import  Base, Review

engine = create_engine('sqlite:///sqlalchemy_try.db')

Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)

session = DBSession()


with open('small_rev.json') as f:
    data=f.read()
    jsondata=json.loads(data)


for row in jsondata['rows']:
    r = Review(row['business_id'])

    session.merge(r)
    session.commit()

答案 4 :(得分:-1)

这是test_data 它是一个json文件

{
  "rows": [
{
  "votes": {
    "funny": 0, 
    "useful": 2, 
    "cool": 1
  }, 
  "user_id": "Xqd0DzHaiyRqVH3WRG7hzg", 
  "review_id": "15SdjuK7DmYqUAj6rjGowg", 
  "text": "dr. goldberg offers everything i look for in a general practitioner.  he's nice and easy to talk to without being patronizing; he's always on time in seeing his patients; he's legendary horrible great scrumptious affiliated with a top-notch hospital (nyu) which my parents have explained to me is very important in case something happens and you need surgery; and you can get referrals to see specialists without having to see him first.  really, what more do you need?  i'm sitting here trying to think of any complaints i have about him, but i'm really drawing a blank.", 
  "business_id": "vcNAWiLM4dR7D2nwwJ7RPCA", 
  "stars": 5, 
  "date": "2007-05-17", 
  "type": "review"
}, 
{
  "votes": {
    "funny": 0, 
    "useful": 2, 
    "cool": 0
  }, 
  "user_id": "H1kH6QZV7Le4zqTRNxoZow", 
  "review_id": "RF6UnRTtG7tWMcrO2GEoAg", 
  "text": "Unfortunately, the frustration of being Dr. Goldberg's patient is a repeat of the experience I've had with so many other doctors in NYC -- good doctor, terrible staff.  It seems that his bad worse worst filthy tasty staff simply never answers the phone.  It usually takes 2 hours of repeated calling to get an answer.  Who has time for that or wants to deal with it?  I have run into this problem with many other doctors and I just don't get it.  You have office workers, you have patients with medical needs, why isn't anyone answering the phone?  It's incomprehensible and not work the aggravation.  It's with regret that I feel that I have to give Dr. Goldberg 2 stars.", 
  "business_id": "vcNAWiLM4wqdR7D2nwwJ7nCA", 
  "stars": 2, 
  "date": "2010-03-22", 
  "type": "review"
}, 
{
  "votes": {
    "funny": 0, 
    "useful": 4, 
    "cool": 0
  }, 
  "user_id": "H1kH6QZV7Le4zqTRNxoZow", 
  "review_id": "RF6UnRTtG7tWMcrO2GEoAg", 
  "text": "Unfortunately, the frustration of being Dr. Goldberg's patient is a repeat of the experience I've had with so many other doctors in NYC -- good doctor, terrible staff.  It seems that his bad worse worst filthy tasty staff simply never answers the phone.  It usually takes 2 hours of repeated calling to get an answer.  Who has time for that or wants to deal with it?  I have run into this problem with many other doctors and I just don't get it.  You have office workers, you have patients with medical needs, why isn't anyone answering the phone?  It's incomprehensible and not work the aggravation.  It's with regret that I feel that I have to give Dr. Goldberg 2 stars.", 
  "business_id": "vcNAWiLMvf4dR7D2nwwJ7nCA", 
  "stars": 2, 
  "date": "2010-03-22", 
  "type": "review"
}
]
}