OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why doesn't use_exisiting_column parameter resolve conflicts here?

  • Thread starter Thread starter Miles Johnson
  • Start date Start date
M

Miles Johnson

Guest
I'm using declarative single table inheritance for a common User base class with different user types broken into derived classes. Two of the derived classes have an identically named and -typed foreign key field, so I used the use_existing_column parameter set to True. Running migrate on the models produces:

sqlalchemy.exc.ArgumentError: Column 'company_id' on class SystemUser conflicts with existing column 'user.company_id'. If using Declarative, consider using the use_existing_column parameter of mapped_column() to resolve conflicts.

I tried each field from their respective models individually and the migrate command executed successfully. I tried adding the field to the common base class and received the same error. Removing the foreign_key parameter from mapped_column didn't change anything either.

Code:
from typing import Optional, List
import sqlalchemy as sa
import sqlalchemy.orm as so
from app import db

class Company(db.Model):
    id: so.Mapped[int] = so.mapped_column(primary_key=True)
    #
    # More stuff
    #

class User(UserMixin, db.Model):
    __tablename__ = "user"
    id: so.Mapped[int] = so.mapped_column(primary_key=True)
    role : so.Mapped[str] = so.mapped_column(sa.String(16))
    #
    # More stuff
    #

    __mapper_args__ = {
        "polymorphic_identity": "user",
        "polymorphic_on": role,
    }

    def __repr__(self):
        return f"{self.__class__.__name__}:{self.email}:{self.role}"


# A System User is a User
# A System User has a Company
class SystemUser(User):
    company_id: so.Mapped[int] = so.mapped_column(sa.ForeignKey("company.id"), nullable=True, use_existing_column=True)

    __mapper_args__ = {
        "polymorphic_identity": "system_user",
    }


# A Loan Officer is a User
# A Loan Officer has a Company
class LoanOfficer(User):
    company_id: so.Mapped[int] = so.mapped_column(sa.Integer, sa.ForeignKey("company.id"), nullable=True, use_existing_column=True)
    company: so.Mapped[Company] = so.relationship(back_populates="loan_officers")
    #
    # More stuff
    #

    __mapper_args__ = {
        "polymorphic_identity": "loan_officer",
    }

pyproject.toml shows that packages are up-to-date:

Code:
[tool.poetry]
name = "mortgageplus"
version = "0.1.0"
description = ""
authors = ["Miles Johnson <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"
Flask = "^3.0.3"
Flask-WTF = "^1.2.1"
python-dotenv = "^1.0.1"
flask-sqlalchemy = "^3.1.1"
pymysql = "^1.1.1"
flask-login = "^0.6.3"
sqlalchemy-utils = "^0.41.2"
flask-migrate = "^4.0.7"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
<p>I'm using declarative single table inheritance for a common <code>User</code> base class with different user types broken into derived classes. Two of the derived classes have an identically named and -typed foreign key field, so I used the <code>use_existing_column</code> parameter set to True. Running migrate on the models produces:</p>
<blockquote>
<p>sqlalchemy.exc.ArgumentError: Column 'company_id' on class SystemUser conflicts with existing column 'user.company_id'. If using Declarative, consider using the use_existing_column parameter of mapped_column() to resolve conflicts.</p>
</blockquote>
<p>I tried each field from their respective models individually and the migrate command executed successfully. I tried adding the field to the common base class and received the same error. Removing the <code>foreign_key</code> parameter from <code>mapped_column</code> didn't change anything either.</p>
<pre><code>from typing import Optional, List
import sqlalchemy as sa
import sqlalchemy.orm as so
from app import db

class Company(db.Model):
id: so.Mapped[int] = so.mapped_column(primary_key=True)
#
# More stuff
#

class User(UserMixin, db.Model):
__tablename__ = "user"
id: so.Mapped[int] = so.mapped_column(primary_key=True)
role : so.Mapped[str] = so.mapped_column(sa.String(16))
#
# More stuff
#

__mapper_args__ = {
"polymorphic_identity": "user",
"polymorphic_on": role,
}

def __repr__(self):
return f"{self.__class__.__name__}:{self.email}:{self.role}"


# A System User is a User
# A System User has a Company
class SystemUser(User):
company_id: so.Mapped[int] = so.mapped_column(sa.ForeignKey("company.id"), nullable=True, use_existing_column=True)

__mapper_args__ = {
"polymorphic_identity": "system_user",
}


# A Loan Officer is a User
# A Loan Officer has a Company
class LoanOfficer(User):
company_id: so.Mapped[int] = so.mapped_column(sa.Integer, sa.ForeignKey("company.id"), nullable=True, use_existing_column=True)
company: so.Mapped[Company] = so.relationship(back_populates="loan_officers")
#
# More stuff
#

__mapper_args__ = {
"polymorphic_identity": "loan_officer",
}
</code></pre>
<p>pyproject.toml shows that packages are up-to-date:</p>
<pre><code>[tool.poetry]
name = "mortgageplus"
version = "0.1.0"
description = ""
authors = ["Miles Johnson <[email protected]>"]
readme = "README.md"

[tool.poetry.dependencies]
python = "^3.10"
Flask = "^3.0.3"
Flask-WTF = "^1.2.1"
python-dotenv = "^1.0.1"
flask-sqlalchemy = "^3.1.1"
pymysql = "^1.1.1"
flask-login = "^0.6.3"
sqlalchemy-utils = "^0.41.2"
flask-migrate = "^4.0.7"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
</code></pre>
 

Latest posts

Top