1

I have created the following model:

class Person(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(1024), nullable=False)
    email = db.Column(db.String(250), nullable=True, unique=True)
    code = db.Column(db.String(10), nullable=False, unique=True)

There are some cases in which a Person turnos into a system user. So, I have the following model:

class User(Person):
    password = db.Column(db.String(1024), nullable=False)

The problem, I that when I try to create an User from a Person doing this:

>>> person = Person(name='foobar', email='[email protected]', code='123123')
>>> db.session.add(person)
>>> db.session.commit()
>>> print person.id
4
>>> user = User(id=4, password='changeme')
>>> db.session.add(user)
>>> db.session.commit()
(IntegrityError) null value in column "name" violates not-null constraint
'INSERT INTO person (id, name, ....

The question is how can I prevent of the model User creating a new Person instance because that already exists on the database?


UPDATE: when trying Join table inheritance, I have the same problem. It don't understand how not to create a new instance of Person

class Person(db.Model):

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(1024), nullable=False)
    email = db.Column(db.String(250), nullable=True, unique=True)
    code = db.Column(db.String(10), nullable=False, unique=True)
    person_type = db.Column(db.String(10), nullable=False)


    __mapper_args__ = {'polymorphic_on': 'person_type'}

class User(Person):
    __mapper_args__ = {'polymorphic_identity': 'recurvado'}

    id = db.Column(db.Integer, db.ForeignKey('arco.id'), primary_key=True)
    password = db.Column(db.String(1024), nullable=False)

The problem, I that when I try to create an User from a Person doing this:

>>> person = Person(name='foobar', email='[email protected]', code='123123')
>>> db.session.add(person)
>>> db.session.commit()
>>> print person.id
4
>>> user = User(id=4, password='changeme')
>>> db.session.add(user)
>>> db.session.commit()
(IntegrityError) null value in column "name" violates not-null constraint
'INSERT INTO person (id, name, .
3
  • I think the error states the issue right away: You create a new User but you haven't specified a name, a code and whatever else has a NOT NULL constraint. From your example I understand that you just want to do this: user = User(name='foobar', email='foobar...', password='changeme'); session.add(user); session.commit() The Person class does not need to be involved. Also, I don't think you can have a non-null password when using joined inheritance, all your Persons will raise integrity errors too Commented Jul 29, 2014 at 20:01
  • The question that I have is: if I have already created a Person, who can I create an User from it without creating a new Person. Commented Jul 30, 2014 at 11:11
  • Haven't tried this upgrading before. I would try like this: user = session.query(User).get(4); user.password = '...' If not, perhaps this: person = session.query(Person).get(4); person.person_type = 'recurvado' ;session.commit() user = session.query(User).get(4) Commented Jul 31, 2014 at 9:41

1 Answer 1

1

There are a couple of ways. The simple one

class Person(object): #notice there's no db.Model here
    id = db.Column(db.Integer, primary_key=True)
    #the rest of the person columns follow

class User(Person):
    password = db.Column(db.String(1024), nullable=False)
    #other user specific attributes and columns

The above is good if you never want your Person class to be instantiated as a db object itself. If you actually want that, you should look into http://docs.sqlalchemy.org/en/rel_0_9/orm/inheritance.html#single-table-inheritance and pick what's best for you, the documentation is extensive.

Sign up to request clarification or add additional context in comments.

1 Comment

Updated the questing with some example

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.