gpineda-dev opened a new issue, #21977:
URL: https://github.com/apache/superset/issues/21977

   Hello, here I am .. my first issue on GitHub I hope to fulfill correctly the 
requirements, otherwise, feel free to mention it to help improving myself.
   
   After spending time replicating over hours the creation of charts, I wanted 
to automate the process. 
   Through the exposed **API** I am now able to create a database, a new 
dataset however I meet some errors while trying to **generate a chart linked to 
an existing database**.
   Indeed as described in the swagger documentation, it should be possible to 
specify the existing dataset id with its type to instantiate a new chart linked 
to it by setting `datasource_type` to `dataset` however this led to a `Fatal 
Error` code.
   
   Then I followed the error stack to target precisely this error :
   - 
[superset.models.slice](https://github.com/apache/superset/blob/bf001931c8c7e58a211e411fa74ca4991c6aa2a8/superset/models/slice.py#L354)
 requires a `.perm` / `schema_perm` 
   - property on fetched 
[`src_class`](https://github.com/apache/superset/blob/bf001931c8c7e58a211e411fa74ca4991c6aa2a8/superset/models/slice.py#L127)
 
   - `src_class` is coming from 
[`superset.datasource.dao.DatasourceDAO.sources`](https://github.com/apache/superset/blob/bf001931c8c7e58a211e411fa74ca4991c6aa2a8/superset/datasource/dao.py#L38)
 and corresponding type should be `Type["BaseDatasource"]`
   -  
[`superset.dataset.models.Dataset`](https://github.com/apache/superset/blob/bf001931c8c7e58a211e411fa74ca4991c6aa2a8/superset/datasets/models.py#L73)
 is not a child class of `BaseDatasource` .. as a result, `.perm` is not an 
available property.
   as mentioned the `Dataset` model [`shouldn't be used 
yet`](https://github.com/apache/superset/blob/bf001931c8c7e58a211e411fa74ca4991c6aa2a8/superset/datasets/models.py#L20).
   
   In my case, I need a virtual dataset since tables name is not unique within 
multiple databases.
   
   #### How to reproduce the bug
   
   > I use https://pypi.org/project/superset-api-client/ as client but I tried 
the same with native python lib + curl.
   
   Please find below a complete example including the use of the client (this 
may help others to understand this REST interface).
   
   ```python
   import os
   from supersetapiclient.databases import Database
   from requests import Response
   
   # Prepare the client
   os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
   client = SupersetClient(
       host='http://localhost:8080',
       password = 'admin',
       username = 'admin',
   )
   
   # Setup the db
   n_database = {
       'database_name': 'issue-github',
       'sqlalchemy_uri': 
'mysql+mysqldb://myuser:[email protected]:3362/mediator-db',
       'expose_in_sqllab': True,
       'allow_ctas': True,
       'allow_cvas': True,
       'allow_dml' : True
   }
   
   rep: Response = client.post(client.databases.base_url, json = n_database )
   
   # Setup a dataset + sync columns
   db = client.databases.find_one(database_name=n_database['database_name'])
   
   n_dataset = {
       'table_name': 'mediator_log',
       'schema': 'mediator-db',
       'database': db.id,
       # the following commented lines are not yet supported by the api ... 
unfortunately 
       # 'kind': 'virtual',
       # 'sql': 'SELECT * FROM `mediator-db`.mediator_log',
   }
   
   rep: Response = client.post(client.datasets.base_url, json= n_dataset)
   dset = client.datasets.find_one(table_name = n_dataset['table_name'])
   dset.table_name = 'issue-github'
   dset.kind = 'virtual'
   dset.sql   = 'SELECT * FROM mediator_log'
   dset.save()
   client.put(client.datasets.base_url + f'/{k.id}/refresh')
   
   
   # Try to create a new chart linked to the previously generated dataset
   n_chart = {
       'datasource_id': dset.id,
       'datasource_type': 'dataset',
       'slice_name': 'chartX',
       # 'params': json.dumps({
       #     'groupby': ['fwd_port', 'uses_internet_proxy'], 
       #     'metric': 'count'
       # }),
       'description': 'ici vient ma description',
       'viz_type': 'pie'
   }
   
   k: Response = client.post(client.charts.base_url, json = n_chart) 
   print(k.json()) # ERROR returned
   ```
   
   
   ### Expected results
   
   As described in the swagger documentation : 
   > 
![grafik](https://user-images.githubusercontent.com/36532817/198905310-ad1914f5-c491-49cc-b889-b8f0ac7af10d.png)
   
   
   ### Actual results
   
   > {'message': 'Fatal error'}
   
   **Superset logs :**
   ```log
   2022-10-30 21:53:48,001:ERROR:root:'Dataset' object has no attribute 'perm'
   Traceback (most recent call last):
     File 
"/usr/local/lib/python3.8/site-packages/flask_appbuilder/api/__init__.py", line 
86, in wraps
       return f(self, *args, **kwargs)
     File "/app/superset/views/base_api.py", line 114, in wraps
       raise ex
     File "/app/superset/views/base_api.py", line 111, in wraps
       duration, response = time_function(f, self, *args, **kwargs)
     File "/app/superset/utils/core.py", line 1574, in time_function
       response = func(*args, **kwargs)
     File "/app/superset/utils/log.py", line 244, in wrapper
       value = f(*args, **kwargs)
     File "/app/superset/views/base_api.py", line 84, in wraps
       return f(self, *args, **kwargs)
     File "/app/superset/charts/api.py", line 304, in post
       new_model = CreateChartCommand(item).run()
     File "/app/superset/charts/commands/create.py", line 48, in run
       chart = ChartDAO.create(self._properties)
     File "/app/superset/dao/base.py", line 131, in create
       db.session.commit()
     File "<string>", line 2, in commit
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 1435, in commit
       self._transaction.commit(_to_root=self.future)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 829, in commit
       self._prepare_impl()
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 808, in _prepare_impl
       self.session.flush()
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 3367, in flush
       self._flush(objects)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 3507, in _flush
       transaction.rollback(_capture_exception=True)
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 
70, in __exit__
       compat.raise_(
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", 
line 207, in raise_
       raise exception
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/session.py", 
line 3467, in _flush
       flush_context.execute()
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 
456, in execute
       rec.execute(self)
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 
630, in execute
       util.preloaded.orm_persistence.save_obj(
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 
212, in save_obj
       for (
     File 
"/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 
385, in _organize_states_for_save
       mapper.dispatch.before_insert(mapper, connection, state)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/event/attr.py", 
line 343, in __call__
       fn(*args, **kw)
     File "/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/events.py", 
line 743, in wrap
       fn(*arg, **kw)
     File "/app/superset/models/slice.py", line 354, in set_related_perm
       target.perm = ds.perm
   AttributeError: 'Dataset' object has no attribute 'perm'
   172.17.0.1 - - [30/Oct/2022:21:53:48 +0000] "POST /api/v1/chart/ HTTP/1.1" 
500 26 "http://localhost:8080/api/v1"; "python-requests/2.28.1"
   ```
   
   
   #### Screenshots
   
   
   
   ### Environment
   
   - browser type and version:  `python-requests/2.28.1`
   - superset version: `Superset 0.0.0-dev`
   - python version: `3.8.13`
   - node.js version: `??`
   - any feature flags active:
        - SUPERSET_WEBSERVER_TIMEOUT=600
        - FAB_API_SWAGGER_UI=1
        - ROW_LIMIT=150000
   
   ### Checklist
   
   Make sure to follow these steps before submitting your issue - thank you!
   
   - 
   
   - [x]  I have checked the superset logs for python stacktraces and included 
it here as text if there are any.
   
   - [x] I have reproduced the issue with at least the latest released version 
of superset.
   - [x] I have checked the issue tracker for the same issue and I haven't 
found one similar.
   
   ### Additional context
   
   Running in docker, image 
`apache/superset@sha256:5fab9c506b7e0e92a94d77568959ac37f898756914cdda36723719cb5758af60`
 pulled from dockerhub
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to