{"id":5545,"date":"2021-06-30T18:46:26","date_gmt":"2021-06-30T09:46:26","guid":{"rendered":"https:\/\/www.sria.co.jp\/blog\/?p=5545"},"modified":"2021-06-30T18:46:26","modified_gmt":"2021-06-30T09:46:26","slug":"5545","status":"publish","type":"post","link":"https:\/\/www.sria.co.jp\/blog\/2021\/06\/5545\/","title":{"rendered":"SQLAlchemy\u306e\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u30c4\u30fc\u30eb Alambic\u5c0e\u5165\u6642\u306e\u30c8\u30e9\u30d6\u30eb\u30b7\u30e5\u30fc\u30c6\u30a3\u30f3\u30b0\u307e\u3068\u3081"},"content":{"rendered":"<p><span style=\"color: #000000\">SQLAlchemy\u3068<\/span><span style=\"color: #000000\">Alembic \u3092\u5c0e\u5165\u3057\u305f\u969b\u3001\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3053\u3068\u304c\u591a\u304b\u3063\u305f\u306e\u3067\u3001\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/span><\/p>\n<p>SQLAlchemy\u3067\u306f\u30b3\u30de\u30f3\u30c9\u3067\u306e\u64cd\u4f5c\u3084Alter Table\u306a\u3069\u306f\u884c\u3048\u307e\u305b\u3093\u304c\u3001<\/p>\n<p>Alembic\u3067\u306f\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u305f\u30b9\u30ad\u30fc\u30de\u306e\u64cd\u4f5c\u3084Alter Table\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u3001\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<h2>\u76ee\u6b21<\/h2>\n<ol>\n<li>sqlalchemy.exc.ArgumentError<\/li>\n<li>sqlalchemy.exc.InvalidRequestError<\/li>\n<li>AttributeError<\/li>\n<li>ImportError<\/li>\n<li>ERROR [alembic.util.messaging]<\/li>\n<\/ol>\n<h2>SQLAlchemy \u3068\u306f<\/h2>\n<p>Python\u306e<a href=\"https:\/\/ja.wikipedia.org\/wiki\/%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E9%96%A2%E4%BF%82%E3%83%9E%E3%83%83%E3%83%94%E3%83%B3%E3%82%B0\">ORM<\/a>(Object Relational Mapper)\u3067\u3059\u3002<\/p>\n<h2>alembic \u3068\u306f<\/h2>\n<p>SQLAlchemy \u3092\u4f7f\u7528\u3057\u305f \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9migration\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<h2>1. sqlalchemy.exc.ArgumentError<\/h2>\n<h3>1-1<\/h3>\n<blockquote><p>sqlalchemy.exc.ArgumentError: Mapper mapped class User-&gt;users could not assemble any primary key columns for mapped table &#8216;users&#8217;<\/p><\/blockquote>\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306f\u30ab\u30f3\u30de\u3092\u3064\u3051\u3066\u3044\u305f\u308a\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>class UserDataModel(Base):\n  __tablename__ = 'users'\n\n  id = Column(Integer(), primary_key=True, autoincrement=True),\n  name = Column(String(100), nullable=False),\n  password = Column(String(255), nullable=False)<\/code><\/pre>\n<\/div>\n<p>Column()\u306e\u4e2d\u306b\u30ab\u30e9\u30e0\u540d\u3092\u66f8\u3044\u3066\u3044\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u539f\u56e0\u3067\u8d77\u304d\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>class UserDataModel(Base):\n  __tablename__ = 'users'\n\n  Column('id', Integer(), primary_key=True, autoincrement=True)\n  Column('name', String(100), nullable=False)\n  Column('password', String(255), nullable=False)<\/code><\/pre>\n<\/div>\n<p><strong>\u89e3\u6c7a\u6cd5<\/strong>\u3068\u3057\u3066\u306f\u30ab\u30f3\u30de\u3092\u4ed8\u3051\u305a\u300cid = Column()\u300d\u306e\u3088\u3046\u306a\u5f62\u3067\u5b9a\u7fa9\u3057\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u3044\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>class UserDataModel(Base):\n\u3000\u3000__tablename__ = 'users'\n\u00a0\n\u3000\u3000id = Column(Integer(), primary_key=True, autoincrement=True)\n\u3000\u3000name = Column(String(100), nullable=False)\n\u3000\u3000password = Column(String(255), nullable=False)<\/code><\/pre>\n<\/div>\n<h3>1-2<\/h3>\n<blockquote><p>sqlalchemy.exc.ArgumentError: Error creating backref &#8216;users&#8217; on relationship &#8216;UserDataModel.reports&#8217;: property of that name exists on mapper &#8216;mapped class ReportDataModel-&gt;reports&#8217;<\/p><\/blockquote>\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306frelationship\u306ebackref\u3067\u5225\u306e\u30e2\u30c7\u30eb\u3067\u5b9a\u7fa9\u3055\u308c\u305frelationship\u306e\u5909\u6570\u540d\u3092\u5b9a\u7fa9\u3057\u305f\u969b\u306b\u8d77\u304d\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>class UserDataModel(Base):\n\u3000\u3000__tablename__ = 'users'\n\n\u3000\u3000id = Column(Integer(), primary_key=True, autoincrement=True)\n\u3000\u3000name = Column(String(100), nullable=False)\n\n\u3000\u3000# \u3053\u3053\u306erelationship\u306ebackref\u3067users\u306f\u5b9a\u7fa9\u3067\u304d\u306a\u3044\n\u3000\u3000reports = relationship(\"ReportDataModel\", backref=\"users\")\n\nclass ReportDataModel(Base):\n\u3000\u3000__tablename__ = 'reports'\n\n\u3000\u3000id = Column(Integer, primary_key=True, autoincrement=True)\n\u3000\u3000user_id = Column(Integer, ForeignKey(\"users.id\"), nullable=False)\n\n\u3000\u3000## \u3053\u3053\u3067users\u3092\u5ba3\u8a00\u3059\u308b\u3068\u4ed6\u306e\u30e2\u30c7\u30eb\u3067\u5b9a\u7fa9\u3055\u308c\u308brelationship\u306ebackref\u306busers(\u540c\u540d)\u306f\u4f7f\u3048\u306a\u304f\u306a\u308b\u3002\n\u3000\u3000users = relationship(\"ReportDetailDataModel\")\n\u3000\u3000report_detail = relationship(\"ReportDetailDataModel\", backref=\"reports\")<\/code><\/pre>\n<\/div>\n<p><strong>\u89e3\u6c7a\u6cd5<\/strong>\u3068\u3057\u3066\u306f\u5225\u540d\u3067\u5b9a\u7fa9\u3059\u308b\u3001\u3082\u3057\u304f\u306fbackref\u3092\u7528\u3044\u3066\u3044\u306a\u3044relationship\u3092\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u30021\u5bfe\u591a\u306e1\u5074\u3067backref\u3092\u7528\u3044\u3066\u3044\u308b\u969b\u306f\u3001\u591a\u5074\u3067\u306frelationship\u306f\u4e0d\u8981\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/backref.html\">https:\/\/docs.sqlalchemy.org\/en\/14\/orm\/backref.html<\/a><\/p>\n<h2>2. sqlalchemy.exc.InvalidRequestError<\/h2>\n<blockquote><p>sqlalchemy.exc.InvalidRequestError: When initializing mapper mapped class UserDataModel-&gt;users, expression &#8216;ReportDataModel&#8217; failed to locate a name (&#8216;ReportDataModel&#8217;). If this is a class name, consider adding this relationship() to the &lt;class &#8216;db.models.user.UserDataModel&#8217;&gt; class after both dependent classes have been defined.<\/p><\/blockquote>\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306frelationship\u306e\u7b2c\u4e00\u5f15\u6570\u306b\u6587\u5b57\u5217\u3067\u30af\u30e9\u30b9\u3092\u6307\u5b9a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u304c\u3001 import \u3057\u306a\u3044\u3053\u3068\u3067\u8d77\u3053\u308a\u307e\u3059\u3002<\/p>\n<p><strong>\u89e3\u6c7a\u6cd5<\/strong>\u3068\u3057\u3066\u306f\u3001relationship \u306e\u7b2c\u4e00\u5f15\u6570\u3067\u6587\u5b57\u5217\u3068\u3057\u3066\u6307\u5b9a\u3057\u305f\u30af\u30e9\u30b9\u3092 import \u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from report import ReportDataModel\nreports = relationship(\"ReportDataModel\", backref=\"user_data_model\")<\/code><\/pre>\n<\/div>\n<h2>3. AttributeError<\/h2>\n<blockquote><p>AttributeError: &#8216;NoneType&#8217; object has no attribute &#8216;fullname&#8217;<\/p><\/blockquote>\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306f SQLAlchemy \u306e\u30e2\u30c7\u30eb\u304c Base \u3092\u7d99\u627f\u3057\u3066\u3044\u306a\u3044\u3068\u8d77\u3053\u308a\u307e\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sqlalchemy import Column, String, Integer\n\nclass UserModel:\n\u00a0 \u00a0 __tablename__ = 'users'\n\u00a0 \u00a0 id = Column(Integer, primary_key=True)\n\u00a0 \u00a0 name = Column(String(100), nullable=False)<\/code><\/pre>\n<\/div>\n<p><strong>\u89e3\u6c7a\u6cd5<\/strong>\u3068\u3057\u3066\u306fBase\u3092\u7d99\u627f\u3057\u3066\u3042\u3052\u307e\u3057\u3087\u3046\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from sqlalchemy import Column, String, Integer\nfrom setting import Base\n\nclass UserModel(Base):\n\u00a0 \u00a0 __tablename__ = 'users'\n\u00a0 \u00a0 id = Column(Integer, primary_key=True)\n\u00a0 \u00a0 name = Column(String(100), nullable=False)<\/code><\/pre>\n<\/div>\n<h2>4. ImportError<\/h2>\n<blockquote><p>ImportError: cannot import name &#8216;UserDataModel&#8217; from partially initialized module &#8216;db.models.user&#8217; (most likely due to a circular import)<\/p><\/blockquote>\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306f\u5faa\u74b0\u53c2\u7167(\u547c\u3073\u51fa\u3057\u5148\u3067\u547c\u3073\u51fa\u3057\u5143\u304c\u547c\u3073\u51fa\u3055\u308c\u3066\u3044\u308b)\u3092\u3057\u3066\u3044\u308b\u6642\u306b\u8d77\u304d\u307e\u3059\u3002<\/p>\n<p><strong>\u89e3\u6c7a\u6cd5<\/strong>\u3068\u3057\u3066\u306f\u3001TYPE_CHECKING\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>TYPE_CHECKING\u306b\u3064\u3044\u3066\u306f\u3001<a href=\"https:\/\/zenn.dev\/ganariya\/articles\/python-lazy-annotation\">\u3053\u3061\u3089<\/a>\u306b\u8a73\u3057\u304f\u66f8\u304b\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h2>5. ERROR [alembic.util.messaging]<\/h2>\n<blockquote><p>ERROR [alembic.util.messaging] Can&#8217;t locate revision identified by &#8216;90333886164c&#8217;<br \/>\nINFO [sqlalchemy.engine.Engine] [generated in 0.00011s] {}<br \/>\nFAILED: Can&#8217;t locate revision identified by &#8216;90333886164c&#8217;<\/p><\/blockquote>\n<p>\u3053\u306e\u30a8\u30e9\u30fc\u306f Alembic \u3067 upgrade \u3057\u3066\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u305f\u5f8c\u306b Alembic\u306edowngrade \u3067\u30c6\u30fc\u30d6\u30eb\u3092\u6d88\u3055\u305a\u306b\u76f4\u63a5 DB \u304b\u3089\u30c6\u30fc\u30d6\u30eb\u3092\u524a\u9664\u3059\u308b\u3068\u3001Alembic \u304c\u4f5c\u6210\u3057\u305f\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u3092\u7ba1\u7406\u3059\u308balembic_version \u30c6\u30fc\u30d6\u30eb\u306b upgrade \u3057\u305f\u969b\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u30ec\u30b3\u30fc\u30c9\u304c\u6b8b\u308b\u3053\u3068\u3067\u8d77\u304d\u307e\u3059\u3002<\/p>\n<p><strong>\u89e3\u6c7a\u6cd5<\/strong>\u3068\u3057\u3066\u306f\u3001alembic_vesion \u30c6\u30fc\u30d6\u30eb\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u524a\u9664\u3059\u308b\u304b\u3001alembic_version \u30c6\u30fc\u30d6\u30eb\u81ea\u4f53\u3092\u524a\u9664\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h2>\u7d42\u308f\u308a\u306b<\/h2>\n<p>SQLAlchemy\u306e\u30e2\u30c7\u30eb\u5b9a\u7fa9\u65b9\u6cd5\u304c\uff12\u901a\u308a\u3042\u3063\u305f\u308a\u3001\u30de\u30a4\u30b0\u30ec\u30fc\u30b7\u30e7\u30f3\u306a\u3089\u3067\u306f\u306e\u30a8\u30e9\u30fc\u306a\u3069\u3042\u3063\u305f\u308a\u3067\u3001\u89e3\u6c7a\u306b\u6642\u9593\u3092\u53d6\u3089\u308c\u305f\u306e\u3067\u3001SQLAlchemy\u3068Alembic\u3092\u5c0e\u5165\u3059\u308b\u969b\u306f\u3054\u53c2\u8003\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/9088957\/sqlalchemy-cannot-find-a-class-name\">https:\/\/stackoverflow.com\/questions\/9088957\/sqlalchemy-cannot-find-a-class-name<\/a><\/p>\n<p><a href=\"https:\/\/qiita.com\/kitarikes\/items\/9c5d6cbc557ed62bb512\">https:\/\/qiita.com\/kitarikes\/items\/9c5d6cbc557ed62bb512<\/a><\/p>\n<p><a href=\"https:\/\/zenn.dev\/ganariya\/articles\/python-lazy-annotation\">https:\/\/zenn.dev\/ganariya\/articles\/python-lazy-annotation<\/a><\/p>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/27399602\/sqlalchemy-exc-argumenterror-error-creating-backref\">https:\/\/stackoverflow.com\/questions\/27399602\/sqlalchemy-exc-argumenterror-error-creating-backref<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQLAlchemy\u3068Alembic \u3092\u5c0e\u5165\u3057\u305f\u969b\u3001\u30a8\u30e9\u30fc\u304c\u51fa\u308b\u3053\u3068\u304c\u591a\u304b\u3063\u305f\u306e\u3067\u3001\u7d39\u4ecb\u3057\u307e\u3059\u3002 SQLAlchemy\u3067\u306f\u30b3\u30de\u30f3\u30c9\u3067\u306e\u64cd\u4f5c\u3084Alter Table\u306a\u3069\u306f\u884c\u3048\u307e\u305b\u3093\u304c\u3001 Alembic\u3067\u306f\u30b3\u30de\u30f3\u30c9\u3092\u7528\u3044\u305f\u30b9&#8230;<\/p>\n","protected":false},"author":5,"featured_media":5156,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[247,245,246],"_links":{"self":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/5545"}],"collection":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=5545"}],"version-history":[{"count":62,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/5545\/revisions"}],"predecessor-version":[{"id":5703,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/posts\/5545\/revisions\/5703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media\/5156"}],"wp:attachment":[{"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=5545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=5545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sria.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=5545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}